]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0004.md
Rollup merge of #92310 - ehuss:rustdoc-ice, r=estebank
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0004.md
1 This error indicates that the compiler cannot guarantee a matching pattern for
2 one or more possible inputs to a match expression. Guaranteed matches are
3 required in order to assign values to match expressions, or alternatively,
4 determine the flow of execution.
5
6 Erroneous code example:
7
8 ```compile_fail,E0004
9 enum Terminator {
10     HastaLaVistaBaby,
11     TalkToMyHand,
12 }
13
14 let x = Terminator::HastaLaVistaBaby;
15
16 match x { // error: non-exhaustive patterns: `HastaLaVistaBaby` not covered
17     Terminator::TalkToMyHand => {}
18 }
19 ```
20
21 If you encounter this error you must alter your patterns so that every possible
22 value of the input type is matched. For types with a small number of variants
23 (like enums) you should probably cover all cases explicitly. Alternatively, the
24 underscore `_` wildcard pattern can be added after all other patterns to match
25 "anything else". Example:
26
27 ```
28 enum Terminator {
29     HastaLaVistaBaby,
30     TalkToMyHand,
31 }
32
33 let x = Terminator::HastaLaVistaBaby;
34
35 match x {
36     Terminator::TalkToMyHand => {}
37     Terminator::HastaLaVistaBaby => {}
38 }
39
40 // or:
41
42 match x {
43     Terminator::TalkToMyHand => {}
44     _ => {}
45 }
46 ```