Skip to content

Commit 6c1d2fa

Browse files
est31ehuss
authored andcommitted
Document if/while let chains
1 parent 467f456 commit 6c1d2fa

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
- [Language](rust-2024/language.md)
4343
- [RPIT lifetime capture rules](rust-2024/rpit-lifetime-capture.md)
4444
- [`if let` temporary scope](rust-2024/temporary-if-let-scope.md)
45+
- [`let` chains in `if` and `while`](rust-2024/let-chains.md)
4546
- [Tail expression temporary scope](rust-2024/temporary-tail-expr-scope.md)
4647
- [Match ergonomics reservations](rust-2024/match-ergonomics.md)
4748
- [Unsafe `extern` blocks](rust-2024/unsafe-extern.md)

src/rust-2024/let-chains.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# `let` chains in `if` and `while`
2+
3+
## Summary
4+
5+
- Allow chaining of `let` expressions in the condition operand of `if` and `while`.
6+
7+
## Details
8+
9+
Starting with the 2024 Edition, it is now allowed to have chaining of `let` expressions inside `if` and `while` condition operands,
10+
where chaining refers to `&&` chains. The `let` expressions still have to appear at the top level,
11+
so `if (let Some(hi) = foo || let Some(hi) = bar)` is not allowed.
12+
13+
Before 2024, the `let` had to appear directly after the `if` or `while`, forming a `if let` or `while let` special variant.
14+
Now, `if` and `while` allow chains of one or more `let` expressions, possibly mixed with expressions that are `bool` typed.
15+
16+
```rust,edition2024
17+
fn sum_first_two(nums: &[u8]) -> Option<u8> {
18+
let mut iter = nums.iter();
19+
if let Some(first) = iter.next()
20+
&& let Some(second) = iter.next()
21+
{
22+
first.checked_add(second)
23+
} else {
24+
None
25+
}
26+
}
27+
```
28+
29+
The feature is edition gated due to requiring [if let rescoping], which is a Edition 2024 change.
30+
31+
## Migration
32+
33+
The switch to Edition 2024 doesn't neccessitate any migrations due to this feature,
34+
as it creates a true extension of the set of allowed Rust programs.
35+
36+
[if let rescoping]: temporary-if-let-scope.html

0 commit comments

Comments
 (0)