Unions That Hold One of Many Types
Store alternative shapes in one slot.
One Slot, Many Shapes
Sometimes a value could be an integer or a float, but only one at a time. A union stores exactly one of several possible types in a single slot.
Declaring a Union
You write union and list named fields, each with its own type. The value holds just one of those fields at any moment.
const Number = union { int: i64, float: f64 };All lessons in this course
- Enums for a Fixed Set of Values
- Unions That Hold One of Many Types
- Tagged Unions and switch
- Enum Methods and Backing Integers