Pattern Matching: choose¶
The choose expression performs pattern matching on enums, tuples, and values.
Basic Syntax¶
Matching Enums¶
state Option(T) {
None,
Some(T)
}
choose maybe_value {
Some(x) => {
print(x);
}
None => {
print("No value");
}
}
Matching Result¶
Matching Tuples¶
make (a, b) = get_pair();
choose (a, b) {
(0, 0) => { print("origin"); }
(x, 0) => { print(`on x-axis: ${x}`); }
(0, y) => { print(`on y-axis: ${y}`); }
(x, y) => { print(`point: ${x}, ${y}`); }
}
Matching with Bindings¶
Patterns can bind variables for use in the arm body:
choose result {
Ok(val) => { make x = val; ... } // val bound
Err(e) => { make err = e; ... } // e bound
}
Exhaustiveness¶
For enums, the compiler checks that all variants are covered. Missing a variant is a compile error.