Pattern Matching

The match keyword let you match a value against one or more patterns. The comparisons are done from top to bottom and the first match wins.

The patterns can be simple values, similarly to switch in C and C++:

1. fn main() {
2. let input = 'x';

4. match input {
5. 'q'                   => println!("Quitting"),
6. 'a' | 's' | 'w' | 'd' => println!("Moving around"),
7. '0'..='9'             => println!("Number input"),
8. _                     => println!("Something else"),
9. }
10. }

The _ pattern is a wildcard pattern which matches any value.

Key Points:

  • You might point out how some specific characters are being used when in a pattern
  • | as an or
  • .. can expand as much as it needs to be
  • 1..=5 represents an inclusive range
  • _ is a wild card
  • It can be useful to show how binding works, by for instance replacing a wildcard character with a variable, or removing the quotes around q.
  • You can demonstrate matching on a reference.
  • This might be a good time to bring up the concept of irrefutable patterns, as the term can show up in error messages.