]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0026.md
Rollup merge of #93556 - dtolnay:trailingcomma, r=cjgillot
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0026.md
1 A struct pattern attempted to extract a non-existent field from a struct.
2
3 Erroneous code example:
4
5 ```compile_fail,E0026
6 struct Thing {
7     x: u32,
8     y: u32,
9 }
10
11 let thing = Thing { x: 0, y: 0 };
12
13 match thing {
14     Thing { x, z } => {} // error: `Thing::z` field doesn't exist
15 }
16 ```
17
18 If you are using shorthand field patterns but want to refer to the struct field
19 by a different name, you should rename it explicitly. Struct fields are
20 identified by the name used before the colon `:` so struct patterns should
21 resemble the declaration of the struct type being matched.
22
23 ```
24 struct Thing {
25     x: u32,
26     y: u32,
27 }
28
29 let thing = Thing { x: 0, y: 0 };
30
31 match thing {
32     Thing { x, y: z } => {} // we renamed `y` to `z`
33 }
34 ```