3 The tracking issue for this feature is: [#44109]
5 [#44109]: https://github.com/rust-lang/rust/issues/44109
7 ------------------------
9 The `non_exhaustive` gate allows you to use the `#[non_exhaustive]` attribute
10 on structs, enums and enum variants. When applied within a crate, users of the
11 crate will need to use the `_` pattern when matching enums and use the `..`
12 pattern when matching structs. Enum variants cannot be matched against.
13 Structs and enum variants marked as `non_exhaustive` will not be able to
14 be created normally outside of the defining crate. This is demonstrated
17 ```rust,ignore (pseudo-Rust)
18 use std::error::Error as StdError;
25 impl StdError for Error {
26 fn description(&self) -> &str {
27 // This will not error, despite being marked as non_exhaustive, as this
28 // enum is defined within the current crate, it can be matched
32 Other => "other or unknown error",
38 ```rust,ignore (pseudo-Rust)
41 // This will not error as the non_exhaustive Error enum has been matched with
44 Message(ref s) => ...,
50 ```rust,ignore (pseudo-Rust)
53 pub window_width: u16,
54 pub window_height: u16,
57 // We can create structs as normal within the defining crate when marked as
59 let config = Config { window_width: 640, window_height: 480 };
61 // We can match structs exhaustively when within the defining crate.
62 if let Ok(Config { window_width, window_height }) = load_config() {
67 ```rust,ignore (pseudo-Rust)
70 // We cannot create a struct like normal if it has been marked as
72 let config = Config { window_width: 640, window_height: 480 };
73 // By adding the `..` we can match the config as below outside of the crate
74 // when marked non_exhaustive.
75 let &Config { window_width, window_height, .. } = config;