Help matching on negative integer

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 ?

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!().

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

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.

You can also use match guards:

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

Thank you for your help guys !

I'm sorry, but I'm confused; I thought that ranges were defined as .. not as ...?

... is an old syntax for inclusive range ..= (where end of the range equals the highest number). .. is for exclusive range (where highest number matched is less than the range end).

2 Likes