]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0023.md
Rollup merge of #93556 - dtolnay:trailingcomma, r=cjgillot
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0023.md
1 A pattern attempted to extract an incorrect number of fields from a variant.
2
3 Erroneous code example:
4
5 ```compile_fail,E0023
6 enum Fruit {
7     Apple(String, String),
8     Pear(u32),
9 }
10
11 let x = Fruit::Apple(String::new(), String::new());
12
13 match x {
14     Fruit::Apple(a) => {}, // error!
15     _ => {}
16 }
17 ```
18
19 A pattern used to match against an enum variant must provide a sub-pattern for
20 each field of the enum variant.
21
22 Here the `Apple` variant has two fields, and should be matched against like so:
23
24 ```
25 enum Fruit {
26     Apple(String, String),
27     Pear(u32),
28 }
29
30 let x = Fruit::Apple(String::new(), String::new());
31
32 // Correct.
33 match x {
34     Fruit::Apple(a, b) => {},
35     _ => {}
36 }
37 ```
38
39 Matching with the wrong number of fields has no sensible interpretation:
40
41 ```compile_fail,E0023
42 enum Fruit {
43     Apple(String, String),
44     Pear(u32),
45 }
46
47 let x = Fruit::Apple(String::new(), String::new());
48
49 // Incorrect.
50 match x {
51     Fruit::Apple(a) => {},
52     Fruit::Apple(a, b, c) => {},
53 }
54 ```
55
56 Check how many fields the enum was declared with and ensure that your pattern
57 uses the same number.