Skip to content
Tutorials Center
My learning

Topics

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.

Rust
if let Some(n) = maybe { /* n in scope */ }
while let Some(top) = stack.pop() { /* ... */ }
let Some(n) = maybe else { return };

Try it

Rust if let, while let and let-else

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.

Rust Let chains (edition 2024)

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

Rust
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 let bindings live only inside the block. let-else is 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 let are dropped before the else block runs, so a lock guard from mutex.lock() no longer stays held there. In older editions they lived to the end of the whole if let ... else.
  • Loops. while let Some(x) = iter.next() is what for expands to; use while let when the loop needs to modify the source, such as popping a stack or reading a queue.
  • Exhaustiveness is not checked. if let ignores all other cases silently. If you need every case handled, use match.
  • Clippy suggests if let over a match with one real arm (single_match), and matches! when you only need a boolean.
  • Type of if let is the type of its blocks; both branches must agree when used as an expression.