`if let`
if let runs a block when a value matches one pattern. Use it when you care about a single variant and are willing to ignore the rest.
Detailed Explanation
match maybe { Some(user) => ..., None => {} } is honest and noisy. When the None arm is empty, if let Some(user) = maybe { ... } says the same thing with less syntax. The pattern sits on the left, the value on the right. If the pattern matches, the bindings are in scope for the block. If it does not, the block is skipped, and an optional else runs.
This is not a second boolean if. The condition is a pattern, not a bool. if let Some(user) = maybe asks "does this value look like Some", and if so, unwraps it. if let Ok(config) = parse(raw) is the same idea for Result when you have a fallback and do not want to thread ? through this function.
else is allowed: if let Some(user) = maybe { greet(user) } else { greet_guest() }. else if let chains exist and become unreadable quickly; a match is clearer at the third arm. Nested if let is a smell that you wanted let-else or a match.
let-else is the sibling added for the "unrecoverable miss" case. let Some(user) = maybe else { return }; continues with user in scope, and the else block must diverge — return, break, continue, or panic. Use if let when both sides do real work. Use let-else when a failed match is an early exit.
Why this exists: match is the complete tool, but demanding four lines for "if this is Some, use it" made people write .unwrap() instead. if let is the safe middle. It does not hide a panic, and it does not pretend the other variants were handled.
What it costs: nothing at runtime that match would not cost. The cost is completeness. if let will not warn you when a new variant appears. If the enum grows from two variants to five, this if let still compiles and still ignores the new ones. That is correct when you truly only care about one shape, and a bug when you thought you were covering "the success case" of a type that now has two success cases.
When you reach for if let: one pattern, optional else, no need for exhaustiveness. When you reach for match: more than one pattern, or you want the compiler to keep the arms honest.
Code Examples
fn main() {
let maybe_port: Option<u16> = Some(8080);
if let Some(port) = maybe_port {
println!("listen {port}"); // prints: listen 8080
} else {
println!("no port");
}
let raw = "not-a-number";
if let Ok(n) = raw.parse::<u16>() {
println!("parsed {n}");
} else {
println!("fallback"); // prints: fallback
}
}Common Gotchas
`if let` is not exhaustive
A new enum variant will not break this code. Switch to `match` when every variant needs a decision.
`let-else` is the early-return form
`let Some(x) = maybe else { return };` keeps `x` in the outer scope. `if let` scopes the binding to the block.
Do not nest `if let` three deep
That is a `match` or a sequence of `let-else` exits. Nested `if let` hides the real control flow.
The right-hand side is moved or copied
`if let Some(x) = maybe` moves `maybe` when it is not `Copy`. Use `if let Some(x) = maybe.as_ref()` to borrow.