match — exhaustive pattern matching (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}78let result = match value {9 Some(n) => n * 2,10 None => 0,11};
if let:
else or else if let.1if let Some(value) = value {2 println!("Got: {}", value);3} else {4 println!("Nothing");5}67// Multiple patterns8if let Some(x) = value {9 println!("Got: {}", x);10} else if let None = value {11 println!("Nothing");12}
When to use:
match for complex multi-case pattern matching.if let when you only care about one pattern.if let for simpler fallback handling.Common mistakes:
match — compiler error.if let when multiple cases need handling.else branch when needed.