Hacker News new | ask | show | jobs
by kibwen 43 days ago
It's important to note that within the compilation unit where the enum is defined, a non_exhaustive enum doesn't require the catch-all arm when matching. In other words, this code compiles and checks for exhaustiveness as usual:

    #[non_exhaustive]
    enum Foo {
        Bar,
        Baz
    }
    
    fn main() {
        let foo = Foo::Bar;

        match foo {
            Foo::Bar => (),
            Foo::Baz => ()
            // no `_` arm necessary here
        }
    }
The non_exhaustive attribute only applies to users outside of the compilation unit, because it's a tool for expressing a promise about the external API.