match
Rust provides pattern matching via the match keyword, which can be used like
a C switch.
1. fn main() {
2. let number = 13;
3. // TODO ^ Try different values for `number`
5. println!("Tell me about {}", number);
6. match number {
7. // Match a single value
8. 1 => println!("One!"),
9. // Match several values
10. 2 | 3 | 5 | 7 | 11 => println!("This is a prime"),
11. // Match an inclusive range
12. 13...19 => println!("A teen"),
13. // Handle the rest of cases
14. _ => println!("Ain't special"),
15. }
17. let boolean = true;
18. // Match is an expression too
19. let binary = match boolean {
20. // The arms of a match must cover all the possible values
21. false => 0,
22. true => 1,
23. // TODO ^ Try commenting out one of these arms
24. };
26. println!("{} -> {}", boolean, binary);
27. }
