diff --git a/guide/statements.md b/guide/statements.md index 9c3eba1..78f836b 100644 --- a/guide/statements.md +++ b/guide/statements.md @@ -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. + +```rust +let MyStruct { foo } = ({ + statement; + fun() +}) else { return }; + +let Some(1) = opt +else { return }; + +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 +}; +```