If I try to build the following code:
fn main () {
let my_val: u32 = 42;
match my_val % 2 {
0 => println!("We're even now"),
1 => println!("Well, that's odd"),
}
}
I will have the following error message:
error[E0004]: non-exhaustive patterns: `2_u32..=u32::MAX` not covered
--> src/main.rs:4:11
|
4 | match my_val % 2 {
| ^^^^^^^^^^ pattern `2_u32..=u32::MAX` not covered
|
= note: the matched value is of type `u32`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
6 ~ 1 => println!("Well, that's odd"),
7 ~ 2_u32..=u32::MAX => todo!(),
|
I don't really get it. What is the case represented by 2_u32..=u32::MAX
?
u32
for the return value of%
since that's the type of its operand. Add a case arm_ => unreachable!(),
to silence the error in the quickest way possible or you should invent a strategy that will have type safety while performing that operation. This may sound like an answer given on comments.. but it's actually a workaround – Deeply%
would be allowed to produce in theory, even though in practice it won't, given its arguments) - You covered only 0 and 1 but the result is au32
which has 4294967294 more possible values you don't yet cover. Solution: add a_
case. – Ethno%
isn
, return aUintAtMost<n-1>
), which Rust doesn't and likely won't ever have. – Pamper2_u32..=u32::MAX
means between2
andmax value of an u32
– Berl