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

Document if/while let chains #337

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
- [Language](rust-2024/language.md)
- [RPIT lifetime capture rules](rust-2024/rpit-lifetime-capture.md)
- [`if let` temporary scope](rust-2024/temporary-if-let-scope.md)
- [`let` chains in `if` and `while`](rust-2024/let-chains.md)
- [Tail expression temporary scope](rust-2024/temporary-tail-expr-scope.md)
- [Match ergonomics](rust-2024/match-ergonomics.md)
- [Unsafe `extern` blocks](rust-2024/unsafe-extern.md)
Expand Down
37 changes: 37 additions & 0 deletions src/rust-2024/let-chains.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# `let` chains in `if` and `while`

## Summary

- Allow chaining of `let` expressions in the condition operand of `if` and `while`.

## Details

Starting with the 2024 Edition, it is now allowed to have chaining of `let` expressions inside `if` and `while` condition operands,
where chaining refers to `&&` chains. The `let` expressions still have to appear at the top level,
so `if (let Some(hi) = foo || let Some(hi) = bar)` is not allowed.

Before 2024, the `let` had to appear directly after the `if` or `while`, forming a `if let` or `while let` special variant.
Now, `if` and `while` allow chains of one or more `let` expressions, possibly mixed with expressions that are `bool` typed.

<!-- TODO: edition2024 -->
```rust
fn sum_first_two(nums: &[u8]) -> Option<u8> {
let mut iter = nums.iter();
if let Some(first) = iter.next()
&& let Some(second) = iter.next()
{
first.checked_add(second)
} else {
None
}
}
```

The feature is edition gated due to requiring [if let rescoping], which is a Edition 2024 change.

## Migration

The switch to Edition 2024 doesn't neccessitate any migrations due to this feature,
as it creates a true extension of the set of allowed Rust programs.

[if let rescoping]: temporary-if-let-scope.html
Loading