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`
  4. println!("Tell me about {}", number);
  5. match number {
  6. // Match a single value
  7. 1 => println!("One!"),
  8. // Match several values
  9. 2 | 3 | 5 | 7 | 11 => println!("This is a prime"),
  10. // Match an inclusive range
  11. 13...19 => println!("A teen"),
  12. // Handle the rest of cases
  13. _ => println!("Ain't special"),
  14. }
  15. let boolean = true;
  16. // Match is an expression too
  17. let binary = match boolean {
  18. // The arms of a match must cover all the possible values
  19. false => 0,
  20. true => 1,
  21. // TODO ^ Try commenting out one of these arms
  22. };
  23. println!("{} -> {}", boolean, binary);
  24. }