Guards

A match guard can be added to filter the arm.

  1. fn main() {
  2. let pair = (2, -2);
  3. // TODO ^ Try different values for `pair`
  4. println!("Tell me about {:?}", pair);
  5. match pair {
  6. (x, y) if x == y => println!("These are twins"),
  7. // The ^ `if condition` part is a guard
  8. (x, y) if x + y == 0 => println!("Antimatter, kaboom!"),
  9. (x, _) if x % 2 == 1 => println!("The first one is odd"),
  10. _ => println!("No correlation..."),
  11. }
  12. }

See also:

Tuples