liveBook · Manning

Non_exhaustive attribute

As explained in Learn Rust in a Month of Lunches

The non_exhaustive attribute marks a Rust type as potentially gaining additional fields or enum variants in future versions.

The `#[non_exhaustive]` attribute marks a Rust type as open to additional fields or enum variants in future versions. It is used mostly with enums, which remain usable while requiring outside match expressions to include a final fallback check. section 13.1.5

Matching a future-proof enum

When matching an externally defined `#[non_exhaustive]` enum, handle the known variants and then add a final fallback case. That fallback protects the match if a later version adds another variant. section 13.1.5

How Learn Rust in a Month of Lunches covers it

How deep it goes
Defines the attribute and explains its effect on enum matching, but does not detail compiler internals or field-specific behavior.
Examples
Uses externally defined non-exhaustive enums and the required final fallback match case as the practical example.
What it assumes
Assumes familiarity with Rust attributes and enum matching, while explaining the need for a final fallback check.
What you can do after
Apply it to future-proof Rust types and write matches that tolerate newly added enum variants.
How it connects
Connects Rust attributes with enums, enum variants, and pattern matching.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.