-
Notifications
You must be signed in to change notification settings - Fork 55
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
Add let else statements #165
Changes from all commits
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 keyword `else` should either appear one space after the initializer expression, or on a subsequent line with indentation matching the keyword `let`. There should always be a space between `else` and `{`. | ||||||||||
|
||||||||||
If the `else` block contains only a single expression (e.g. `return x` or `continue` or `break x`), the entire statement should appear on one line if it fits. | ||||||||||
|
||||||||||
```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 first, before inserting any line breaks into the initializer or pattern: | ||||||||||
|
||||||||||
```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`, | ||||||||||
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 | ||||||||||
}; | ||||||||||
|
||||||||||
let MyStruct { foo: Some(1) } = | ||||||||||
some_variable | ||||||||||
else { | ||||||||||
return | ||||||||||
}; | ||||||||||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think "line begins with
else
" and "preceded by..." are mutually exclusive. Perhaps another paragraph is needed for that case.