Exhaustive matching
match must handle every possible value:
match color {
Red => {},
Green => {},
Blue => {},
}
If you forget a case, the compiler errors. Use _ to catch anything unlisted. This guarantees no value slips through unhandled.
Match guards
Add conditions to match arms:
match n {
x if x > 0 => println!("positive"),
x if x < 0 => println!("negative"),
_ => println!("zero"),
}
The if after the pattern is a guard. Only arms whose guard is true run, letting you add logic to patterns.