Why does the rust compiler think this match is non-exhaustive


#1

I’m writing rust code to parse discrete values I’m receiving from the network. In many cases, these are values defined by IANA. I’m finding it very frustrating that the rust compiler thinks my match statements are non-exhaustive. For example, here’s a trivial example:-

fn main()
{
    let x: u8 = 5;
    
    match x
    {
        0 => println!("hello 0"),
        1 ... 254 => println!("hello range not used"),
        255 => println!("This should be exhaustive"),
    }
}

I can’t be the only one to have asked this, but why does the rust compiler think this isn’t exhaustive? From a maintainability point of view, it would be dangerous to add the _ match with a panic! or an unreachable!; over several years, code like this is likely to be edited and a case subtly missed (eg when a value in a range is now assigned) - especially so as it’s the kind of task often assigned to the new junior programmer on a team.

Of course, one case list all the cases, but with a u16 or u32 match that’s not sensible… And it also makes the layout of the code diverge substantially from how documentation like that from IANA tends to layout values - again affecting maintainability. (This point might seem minor to some, but it is highly important; modelling code in a way similar to documentation reduces the cognitive burden. No more so that when dealing with 100 page + bond contracts).


#2

This is an unfortunate limitation of the current implementation, which is complicated by the fact that it will be (technically) a breaking change and seems to touch several gnarly areas of the compiler. The concensus was that this should be an RFC.


#3

@lifthrasiir Thanks for the explanation.