-
Notifications
You must be signed in to change notification settings - Fork 55
Add let else statements #165
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -148,3 +148,58 @@ fn bar() { | |||||||||
foo(); | ||||||||||
} | ||||||||||
``` | ||||||||||
|
||||||||||
### Let else statements | ||||||||||
|
||||||||||
The `else` should have once space before and after. It can also be at the beginning of a line with a space after. | ||||||||||
|
||||||||||
If the `else` block only contains only an expression, the entire statement should be on one line if it fits. | ||||||||||
camsteffen marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
```rust | ||||||||||
let Some(1) = opt else { return }; | ||||||||||
``` | ||||||||||
|
||||||||||
If the line needs to be broken, prefer to break after the opening brace of the `else` block | ||||||||||
camsteffen marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
```rust | ||||||||||
let Some(1) = opt else { | ||||||||||
return | ||||||||||
}; | ||||||||||
``` | ||||||||||
|
||||||||||
If the `else` and opening brace do not fit on the same line as the initializer, | ||||||||||
break before `else`. If a line begins with `else`, it should be indented at the same level as `let`, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think "line begins with |
||||||||||
and the block should be on the same line if it fits. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Additional justification: this only applies when the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The style guide does not need to say "why" right? I think what you are suggesting here should not be a dependent clause of "if a line begins with
I'm assuming that we can lean on the "blocks" section of the guide and so "not written in one line" is specific enough. Regardless of how it's worded, I agree, this is more consistent with if/else. |
||||||||||
|
||||||||||
```rust | ||||||||||
let MyStruct { foo } = ({ | ||||||||||
statement; | ||||||||||
fun() | ||||||||||
}) else { return }; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
let Some(1) = opt | ||||||||||
else { return }; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
let Some(1) = opt | ||||||||||
else { | ||||||||||
println!("nope"); | ||||||||||
return; | ||||||||||
}; | ||||||||||
``` | ||||||||||
|
||||||||||
If the last line of the initializer expression is indented past `let`, | ||||||||||
the `else` should be broken to the next line. | ||||||||||
|
||||||||||
```rust | ||||||||||
let Foo { bar } = foo | ||||||||||
.method() | ||||||||||
else { | ||||||||||
return; | ||||||||||
camsteffen marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
}; | ||||||||||
|
||||||||||
let MyStruct { foo: Some(1) } = | ||||||||||
some_variable | ||||||||||
else { | ||||||||||
return; | ||||||||||
camsteffen marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
}; | ||||||||||
``` |
Uh oh!
There was an error while loading. Please reload this page.