Rust Reference ยท Patterns
if let, while let and let-else
Reference for refutable patterns in Rust control flow: if let, while let, let-else for early exits, and let chains that combine patterns with && in edition 2024.
A normal let needs a pattern that always matches. When a pattern can fail, Rust offers three control-flow forms that handle the failure case for you: if let runs a block only when the pattern matches, while let loops as long as it matches, and let ... else binds on success and must diverge (return, break, panic) on failure.
In edition 2024, let chains let you join several let patterns and boolean conditions with && in one if or while, which avoids deep nesting.
if let Some(n) = maybe { /* n in scope */ } while let Some(top) = stack.pop() { /* ... */ } let Some(n) = maybe else { return };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
if let PATTERN = EXPR { ... } else { ... } if let PATTERN = EXPR && COND && let PATTERN2 = EXPR2 { ... } // 2024 while let PATTERN = EXPR { ... } let PATTERN = EXPR else { diverge };
| Form | On match | On no match |
|---|---|---|
if let |
Runs the block with the bindings. | Runs else if present, otherwise skips. |
while let |
Runs the body, then re-evaluates the expression. | Leaves the loop. |
let ... else |
Bindings are available after the statement. | The else block must not fall through (type !). |
Errors and edge cases: the else block of let-else that can fall through is error E0308. The scrutinee expression of let ... else cannot end in } (wrap it in parentheses) and cannot be a lazy boolean such as a && b. Let chains are only accepted in edition 2024; in older editions you get a feature-gate error.
Usage notes
- Scope.
if letbindings live only inside the block.let-elseis the opposite: the bindings are visible for the rest of the enclosing block, which is why it suits early returns and keeps code flat. - Temporaries. In edition 2024 temporaries created in the scrutinee of
if letare dropped before theelseblock runs, so a lock guard frommutex.lock()no longer stays held there. In older editions they lived to the end of the wholeif let ... else. - Loops.
while let Some(x) = iter.next()is whatforexpands to; usewhile letwhen the loop needs to modify the source, such as popping a stack or reading a queue. - Exhaustiveness is not checked.
if letignores all other cases silently. If you need every case handled, usematch. - Clippy suggests
if letover amatchwith one real arm (single_match), andmatches!when you only need a boolean. - Type of
if letis the type of its blocks; both branches must agree when used as an expression.
Related
ifandelse,while,let,matches!patterns- Lessons: if expressions, Patterns, Loops