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

Add let else statements #1

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions guide/statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
```