I've stumbled across a very strange behavior from the Rust compiler:

fn main() {
   match bool_result(false) {
       Err(_) => println!("Got error"),
       Ok(value) if value => println!("Got value TRUE"),
       Ok(value) if !value => println!("Got value FALSE"),
       // Ok(z) => println!("WTF: {}", z), // uncomment to compile
   }
}

fn bool_result(x: bool) -> Result<bool, ()> {
    Ok(x)
}

The snippet above does not compile with the below error unless I uncomment the last match arm. But boolean values have only TRUE/FALSE, so why does rustc think that the match is not exhaustive?

error[E0004]: non-exhaustive patterns: `Ok(_)` not covered
 --> src/main.rs:4:10
  |
4 |    match bool_result(false) {
  |          ^^^^^^^^^^^^^^^^^^^^^^ pattern `Ok(_)` not covered

Rustc:

rustc --version
rustc 1.33.0 (2aa4c46cf 2019-02-28)

1 Answers

4
chpio On Best Solutions

The compiler is just not able to confirm, that you're exhausting the match patterns, that's because you're doing the matching outside of the pattern:

fn main() {
   match bool_result(false) {
       Err(_) => println!("Got error"),
       Ok(true) => println!("Got value TRUE"),
       Ok(false) => println!("Got value FALSE"),
   }
}

fn bool_result(x: bool) -> Result<bool, ()> {
    Ok(x)
}

https://play.integer32.com/?version=stable&mode=debug&edition=2018&gist=1389a76ee657e11eb045a4ffc8da9800