Structs, Enums, and Matching
Pattern Matching
Destructuring and Guards
Patterns can destructure tuples and add if guards, so one match expresses several conditions.
Program
Play the program to classify a point with a match guard.
pattern_match.rs
fn main() {
let point = (2, -3);
let label = match point {
(0, 0) => "origin",
(x, _) if x > 0 => "right",
_ => "other",
};
println!("{label}");
}
destructuring
`(x, _)` pulls the first tuple field into `x`.
match guard
`if x > 0` adds a condition to an arm.
order
Arms are tried top to bottom; the first fit wins.