Slide 18
Slide 18 text
let number = 13;
match number {
1 => println!("One!"), // match single
2 | 3 | 5 | 7 | 11 => println!("This is a prime"), // match OR
13...19 => println!("A teen"), // match range
_ => println!("Ain't special"), // rest of cases
}
let boolean = true;
// match is an expression too!
let binary: u8 = match boolean {
false => 0,
true => 1,
};
let pair = (0, -2);
// match works with tuples, enums, structs, etc aswell (destructuring)
match pair {
(0, y) => println!("First is `0` and `y` is `{:?}`", y),
(x, 0) => println!("`x` is `{:?}` and last is `0`", x),
}
Flow control: pattern matching
18