pattern

Rust Extract one case using if let

Use `if let` when only one pattern needs special handling. Unlike a full match, it does not require listing every alternative. Expected output (not executed): discount: 5 Why: The Some pattern succeeds and introduces amount in its branch. Common mistake: The binding amount is not available after the conditional.

Formal Syntax & Structure

rust syntaxrust
if let pattern = value { ... } else { ... }

Working Example

Example Snippetrust
fn main() {    let coupon = Some(5);    if let Some(amount) = coupon {        println!("discount: {amount}");    } else {        println!("no coupon");    }}
Rust Reference