← Back to Gists

if let Some

📝 Rust
stevenandrews
stevenandrews · Level 4 ·

if let Some in Rust is a concise pattern for matching the Some variant of an Option, executing code only when the value is present and binding the inner value.

Rust
fn main() { let value: Option<i32> = Some(42); if let Some(x) = value { println!("Got {}", x); }
}

Comments

0
dmiller dmiller

if let Some(x) is fine until you need to also handle the None case and realize you should have just used match.