I try to exhaustively match integers like this:
fn main() {
for test in range(std::u8::MIN, std::u8::MAX) {
match test {
0x00..0xff => {},
}
}
}
But the compiler complains:
all.rs:3:9: 6:10 error: non-exhaustive patterns: `_` not covered [E0004]
all.rs:3 match test {
all.rs:4 0x00..0xff => {},
all.rs:5 }
error: aborting due to previous error
However, all possible values are covered. A quick check confirms this:
fn main() {
for test in range(std::u8::MIN, std::u8::MAX) {
match test {
0x00..0xff => {},
_ => fail!("impossible"),
}
}
}
And now compile and run it:
$ rustc all.rs
$ ./all
$
All possible values are covered, so why does rustc still want the obviously unreachable _
arm?
Exhaustive integer matching was stabilised in Rust 1.33.0. The original example in the question now works (updated to use the modern syntax for ranges).
fn main() {
for i in std::u8::MIN..=std::u8::MAX {
match i {
0x00..=0xff => {}
}
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments