File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments