match — exhaustive pattern matching (all cases). if let — single pattern match.
match:
1let value = Some(42);2match value {3 Some(x) if x > 100 => println!("Huge: {}", x),4 Some(x) if x > 10 => println!("Big: {}", x),5 Some(x) => println!("Small: {}", x),6 None => println!("Nothing"),7}89// Returning a value10let label = match value {11 Some(n) => format!("Value: {}", n),12 None => "Empty".into(),13};
if let:
else or else if let.1if let Some(value) = value {2 println!("Got: {}", value);3}45// Chaining6if let Some(x) = value {7 println!("Got: {}", x);8} else {9 println!("Nothing");10}
When to use:
match for complex multi-case pattern matching.if let when you only care about one pattern.if let for cleaner code with simple fallbacks.Common mistakes:
match — compiler error.if let when multiple cases need handling — use match.