match — exhaustive pattern matching covering all cases. if let — matches a single pattern.
match:
if x > 5), multiple patterns, and destructuring.1let num = Some(42);2match num {3 Some(x) if x > 100 => println!("Large: {}", x),4 Some(x) => println!("Small: {}", x),5 None => println!("Nothing"),6}78// Binding in match9let result = match num {10 Some(n) => n * 2,11 None => 0,12};
if let:
else if let or else.1if let Some(value) = num {2 println!("Got: {}", value);3}45// Multiple patterns6if let Some(x) = num {7 println!("Got: {}", x);8} else if let None = num {9 println!("Nothing");10}
When to use:
match for complex pattern matching with multiple cases.if let when you only care about one specific pattern.if let for cleaner code when the fallback is trivial.Common mistakes:
match — compiler error.if let when you need multiple cases — use match.else with if let when the other case matters.