If let and let else
Quick answer
Use if let when only one pattern matters and other cases can be ignored or handled together. Use let else to extract a required value while leaving the current control flow early if the pattern does not match.
Select or require a pattern
if let Some(value) = item { ... } executes its body only for Some. An optional else block handles other cases. let Some(value) = item else { return; }; introduces value into the surrounding scope after success; the else branch must diverge (for example by returning). Use match if several variants need their own meaningful branches.
fn main() { let notice = Some("ready"); if let Some(text) = notice { println!("notice: {text}"); }}Expected stdout (not executed):
notice: readyThe body runs only when a notice exists.
fn show(item: Option<i32>) { let Some(value) = item else { println!("missing"); return; }; println!("value: {value}");}fn main() { show(None); show(Some(8)); }Expected stdout (not executed):
missingvalue: 8The missing branch returns before the later use of value.
Common mistake
A let-else failure branch cannot simply print and continue, because the required binding would not exist.
Try it yourself
Rewrite the optional notice as a match, then create a function with let-else that rejects an absent score.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
If let and let else — practice quiz
Choose one answer per question, then review the explanation.
When does `if let Some(n) = item { println!("{n}"); }` execute its body?
Why must the else branch of `let Some(n) = item else { return; };` diverge?
When is match preferable to if let for an enum?