0PricingLogin
Zig Academy · Lesson

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

  1. Enums for a Fixed Set of Values
  2. Unions That Hold One of Many Types
  3. Tagged Unions and switch
  4. Enum Methods and Backing Integers
← Back to Zig Academy