if let expressions
If you want to match a value against a pattern, you can use if let:
fn main() { let arg = std::env::args().next(); if let Some(value) = arg { println!("Program name: {value}"); } else { println!("Missing name?"); } }
See pattern matching for more details on patterns in Rust.
if letcan be more concise thanmatch, e.g., when only one case is interesting. In contrast,matchrequires all branches to be covered.
- Unlike
match,if letdoes not support guard clauses for pattern matching.