]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0638.md
Rollup merge of #93556 - dtolnay:trailingcomma, r=cjgillot
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0638.md
1 This error indicates that the struct, enum or enum variant must be matched
2 non-exhaustively as it has been marked as `non_exhaustive`.
3
4 When applied within a crate, downstream users of the crate will need to use the
5 `_` pattern when matching enums and use the `..` pattern when matching structs.
6 Downstream crates cannot match against non-exhaustive enum variants.
7
8 For example, in the below example, since the enum is marked as
9 `non_exhaustive`, it is required that downstream crates match non-exhaustively
10 on it.
11
12 ```rust,ignore (pseudo-Rust)
13 #[non_exhaustive]
14 pub enum Error {
15     Message(String),
16     Other,
17 }
18
19 impl Display for Error {
20     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
21         // This will not error, despite being marked as non_exhaustive, as this
22         // enum is defined within the current crate, it can be matched
23         // exhaustively.
24         let display = match self {
25             Message(s) => s,
26             Other => "other or unknown error",
27         };
28         formatter.write_str(display)
29     }
30 }
31 ```
32
33 An example of matching non-exhaustively on the above enum is provided below:
34
35 ```rust,ignore (pseudo-Rust)
36 use mycrate::Error;
37
38 // This will not error as the non_exhaustive Error enum has been matched with a
39 // wildcard.
40 match error {
41     Message(s) => ...,
42     Other => ...,
43     _ => ...,
44 }
45 ```
46
47 Similarly, for structs, match with `..` to avoid this error.