Skip to content

Commit 871aedb

Browse files
committed
Document if/while let chains
1 parent ff1c198 commit 871aedb

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-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](rust-2024/match-ergonomics.md)
4748
- [Unsafe `extern` blocks](rust-2024/unsafe-extern.md)

src/rust-2024/let-chains.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
<!-- TODO: edition2024 -->
17+
```rust
18+
fn sum_first_two(nums: &[u8]) -> Option<u8> {
19+
let mut iter = nums.iter();
20+
if let Some(first) = iter.next()
21+
&& let Some(second) = iter.next()
22+
{
23+
first.checked_add(second)
24+
} else {
25+
None
26+
}
27+
}
28+
```
29+
30+
The feature is edition gated due to requiring [if let rescoping], which is a Edition 2024 change.
31+
32+
## Migration
33+
34+
The switch to Edition 2024 doesn't neccessitate any migrations due to this feature,
35+
as it creates a true extension of the set of allowed Rust programs.
36+
37+
[if let rescoping]: temporary-if-let-scope.html

0 commit comments

Comments
 (0)