Hacker News new | ask | show | jobs
by ghfhghg 469 days ago
Languages like haxe simply won't compile if you don't cover every enum value in a switch case. Would that not be preferable? I quite like that feature.

F# I believe is similar wrt discriminated unions and pattern matching

1 comments

I think you misunderstood.

By default Rust expects you to handle every enum variant. Not doing so would be a compile error.

An example - my library exposes enum Colour with 3 variants - Red, Blue Green. In your application code you `match` on all 3. So far so good. But now if I add a 4th colour to my enum, your code will no longer compile because you are no longer handling every enum variant. This is a crappy experience for the user of the library.

Instead, the library writer can make their intent clear - with the #[non_exhaustive] attribute. On such an enum it's not enough to handle the 3 colours of the enum, you must add a wildcard matcher that matches any variants added in future. This gives the library writer flexibility to make changes, while protecting the application developer from breakage.

Oh I see. I did indeed misunderstand.

Thanks for taking the time to explain!