Union types - The Rust Reference (original) (raw)
Keyboard shortcuts
Press ← or → to navigate between chapters
Press S or / to search in the book
Press ? to show this help
Press Esc to hide this help
The Rust Reference
Union types
A union type is a nominal, heterogeneous C-like union, denoted by the name of a union item.
Unions have no notion of an “active field”. Instead, every union access transmutes parts of the content of the union to the type of the accessed field.
Since transmutes can cause unexpected or undefined behaviour, unsafe is required to read from a union field.
Union field types are also restricted to a subset of types which ensures that they never need dropping. See the itemdocumentation for further details.
The memory layout of a union is undefined by default (in particular, fields do_not_ have to be at offset 0), but the #[repr(...)] attribute can be used to fix a layout.