]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0784.md
Assign E0784 to union expression error
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0784.md
1 A union expression does not have exactly one field.
2
3 Erroneous code example:
4
5 ```compile_fail,E0784
6 union Bird {
7     pigeon: u8,
8     turtledove: u16,
9 }
10
11 let bird = Bird {}; // error
12 let bird = Bird { pigeon: 0, turtledove: 1 }; // error
13 ```
14
15 The key property of unions is that all fields of a union share common storage.
16 As a result, writes to one field of a union can overwrite its other fields, and
17 size of a union is determined by the size of its largest field.
18
19 You can find more information about the union types in the [Rust reference].
20
21 Working example:
22
23 ```
24 union Bird {
25     pigeon: u8,
26     turtledove: u16,
27 }
28
29 let bird = Bird { pigeon: 0 }; // OK
30 ```
31
32 [Rust reference]: https://doc.rust-lang.org/reference/items/unions.html