4 ms·
Match has multiple branches, and some or all of them can bind variables, (borrowed from the value matched against), defined in that branch. For that to be possi
by GolDDranks 2y ago
Match has multiple branches, and some or all of them can bind variables, (borrowed from the value matched against), defined in that branch. For that to be possible, it means that the temporary lifetime of the matched value must encompass all the branches.
Compared to that, if let - else has only two branches: one where matching (and possible variable binding) happens, and one where it doesn't. You couldn't have anything borrowed from the matched value in the else branch, so the lifetime extending to encompass the else branch is not strictly necessary.
- NobodyNada 2y agoAlso, from a pure syntax perspective, an if let block reads like it creates a binding that exists within the scope of the braces after the if, and a match block reads like it creates a binding that exists within the "scope" of the braces surrounding the entire match block. These all read the same to me from a scoping perspective: if let Some(x) = expr { // expr is live here } while let Some(x) = expr { // expr is live here } match expr { // expr is live here Some(x) => {}, _ => { /* expr is still live, doesn't matter that x is inaccessible */ } fn foo(x: i32) { // same idea, the variable x belongs to the scope it's declared alongside } For the scope of an if-let expression to extend into the else block afterwards violates the principle of least surprise for me -- and clearly the author of the OP too! if let Some(x) = expr { // expr is live here } else { // ??? why is expr live here? the if is clearly "out of scope!" }