book icon indicating copy to clipboard operation
book copied to clipboard

ch18-03: Guarded match arm exhaustivness clarification

Open deep-outcome opened this issue 3 years ago • 0 comments
trafficstars

As per experimentation shows, match arms exhaustivness is checked even when arms use match guard.

This simple sample

pub fn test() {
    match Some(5) {
        Some(50) => println!("Got it"),
        Some(_n) if true  => println!("Exhausted"),
        None => (),
    }
}

produces output

Not Exhausted

Notable observations are

  • Arm Some(_n) if true => println!("Exhausted") catches already any other Some other than Some(50).
  • After None => (), change to _ => (),, compilation produces no error.

Obviously compiler does no heuristics on possible results of match guard. But it still acts as if makes strong check on arms exhaustivness.

deep-outcome avatar Oct 22 '22 20:10 deep-outcome