Help matching on negative integer


#1

Hello,
I would like to be able to point at a direction by using a match condition statement (see the playground code here). I wonder what I am doing wrong, could you point my error in the reasoning on the patterns and the way this matching should work ?


#2

Patterns don’t support open-ended ranges, but you can use constants for endpoints, like std::i32::MIN...0. Note that pattern ranges are inclusive, so this also matches 0. If you don’t want that, use MIN...-1.

Another caveat is that rustc doesn’t know how to check exhaustive number matches. So even if you think you’ve covered everything, you’re going to need a final catch-all match – like _ => unreachable!().


#3

Thanks a lot for your extensive response !
Is there a way to avoid citing the type in the MAX/MIN ?


#4

Not directly. You could almost do that with num_traits::Bounded, but those methods would have to be const. We might get similar associated constants in num-traits pr91 though.


#5

You can also use match guards:

 (0, y) if y <= 0 => println!("N")