Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using unwrap_or_default() in place of unwrap_or(<default>) #9435

Open
xphoniex opened this issue Sep 7, 2022 · 1 comment
Open

Using unwrap_or_default() in place of unwrap_or(<default>) #9435

xphoniex opened this issue Sep 7, 2022 · 1 comment

Comments

@xphoniex
Copy link
Contributor

xphoniex commented Sep 7, 2022

Description

Currently this is used:

num.unwrap_or(0);

This is ok for non-default values, but I propose we suggest unwrap_or_default instead for default values of primitive types, which all happen to be 0, and false for bools.

So instead we'd write:

num.unwrap_or_default();

Our syntax is longer, but it'd hard to come up with a case where default value has more characters than keyword "default" itself, which is 7 chars long. If we have an unwrap_or_default method, it makes sense to use it.

Comments please.

Version

No response

Additional Labels

No response

@tbu-
Copy link

tbu- commented Sep 24, 2022

If we have an unwrap_or_default method, it makes sense to use it.

It only makes sense if it makes the intent clearer. I don't see that for most default values, .unwrap_or(false), .unwrap_or(0), .unwrap_or(None) all seem clearer (and shorter!) than .unwrap_or_default().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants