From 5a1f03863adce03d348d4a94642b1d38961fdd5e Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Mon, 4 Oct 2021 18:09:36 -0500 Subject: [PATCH 1/2] Add let-else --- guide/statements.md | 55 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/guide/statements.md b/guide/statements.md index 9c3eba1..7ef296f 100644 --- a/guide/statements.md +++ b/guide/statements.md @@ -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. + +```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 + +```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; +}; +``` From 9aaa0bdf44f916c0c0442cc3c17c6f2d2d7331b3 Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Tue, 5 Oct 2021 12:51:56 -0500 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Josh Triplett --- guide/statements.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/guide/statements.md b/guide/statements.md index 7ef296f..78f836b 100644 --- a/guide/statements.md +++ b/guide/statements.md @@ -151,15 +151,15 @@ fn bar() { ### 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. +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 only contains only an expression, the entire statement should be on one line if it fits. +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 +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 { @@ -194,12 +194,12 @@ the `else` should be broken to the next line. let Foo { bar } = foo .method() else { - return; + return }; let MyStruct { foo: Some(1) } = some_variable else { - return; + return }; ```