30
The Missing Piece in Rust Error Handling
(mcmah309.github.io)
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Credits
Had to just think about why
#[non_exhaustive]exists, so perhaps this helps you as well:Imagine you're developing a really important core library, something like TCP. And then other libraries depend on your library.
Now, if you change your implementation, you may need to introduce a new error case, even though the rest of the API still works the same.
You have two choices to deal with that:
#[non_exhaustive]before going 1.0 and therefore forced all depending libraries to implement a default match case. You can sneak in the new error case in version 1.1 without all those depending libraries needing to manually upgrade. The ecosystem does not split in half between 1.0 vs. 2.0.Obviously, if it's a really egregious new error case that needs specific handling, then you might still want to release version 2.0, but by adding the
#[non_exhaustive], you can decide later on and if you are such a central library, there's definitely going to be situations where you want to make use of it.I have been thinking of semver incorrectly, you're right: https://doc.rust-lang.org/cargo/reference/semver.html#enum-variant-new