match
match compares a value with patterns and must cover every case. _ is the catch all.
Example
fn main() {
let n = 3;
let s = match n {
1 => "one",
2 | 3 => "two or three",
4..=9 => "four to nine",
_ => "other",
};
println!("{}", s);
}Result
two or three
Exercise
Catch all.
=> "x"Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.