]>
Commit | Line | Data |
---|---|---|
94222f64 XL |
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 |