match — exhaustive pattern matching (must cover all cases). if let — single pattern match.
match:
1let value = Some(5);2match value {3 Some(x) if x > 10 => println!("Big: {}", x),4 Some(x) => println!("Small: {}", x),5 None => println!("Nothing"),6}78// Can bind and return values9let description = match value {10 Some(n) => format!("Got {}", n),11 None => "Empty".to_string(),12};
if let:
else for the remaining cases.1let value = Some(5);2if let Some(x) = value {3 println!("Got: {}", x);4}56// With else7if let Some(x) = value {8 println!("Got: {}", x);9} else {10 println!("Nothing");11}
When to use:
match when you need to handle all variants or use complex patterns.if let when you only care about one specific case.if let for cleaner code when the other branches are trivial.Common mistakes:
match — compiler error (exhaustiveness check).if let when you need to handle multiple cases — use match.else with if let when the fallback matters.