Complex Pattern Guards in Rust

Limits of Exhaustive Match checking

It is not generally decidable to check if a guarded pattern is exhaustive. Rust does not really try very hard to check exhaustiveness either.

fn main() {
   let pair = (2, -2);

   println!("Tell me about {:?}", pair);
   match pair {
       (x, y) if x == y => println!("These are twins"),
       (x, y) if x + y == 0 => println!("Antimatter, kaboom!"),
       (x, _) if x % 2 == 1 => println!("The first one is odd"),
       (x, y) if x + y != 0 => println!("No correlation..."),
       // at this point the pattern is exhaustive
       (_, y) if y % 2 == 1 => println!("The second one is odd"),
       // but a wildcard unreachable branch is still necessary
       (_, _) => unreachable!()
    }
}