if let expressions
If you want to match a value against a pattern, you can use if let:
1. fn main() {
2. let arg = std::env::args().next();
3. if let Some(value) = arg {
4. println!("Program name: {value}");
5. } else {
6. println!("Missing name?");
7. }
8. }
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.- For the similar use case consider demonstrating a newly stabilized let else feature.
- A common usage is handling
Somevalues when working withOption. - Unlike
match,if letdoes not support guard clauses for pattern matching.
