Unions That Hold One of Many Types
Store alternative shapes in one slot.
Unions That Hold One of Many Types is a free Zig Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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 };It Holds One Field at a Time
Unlike a struct, a union does not keep every field at once. It stores a single active field, and the others are not present.
Setting the Active Field
You initialize a union by choosing one field with the dot syntax. That field becomes the active one for the value.
var n = Number{ .int = 42 };Reading the Active Field
You read a union through the field you set. Access n.int and you get the integer you stored, just like reaching into a struct.
const x = n.int;Memory Is Shared
A union reserves room for its largest field and reuses that space. This makes it a compact way to hold one of several variants.
Switching the Active Field
Assigning a new field makes that field the active one and the old value goes away. The union now represents a different shape.
n = Number{ .float = 3.14 };Reading the Wrong Field
If you read a field that is not active, that is a bug. In safe builds Zig detects the wrong access and triggers a panic.
const f = n.int; // wrong field activeA Bare Union Has No Tag
A plain union does not remember which field is active. You must track that yourself, which is exactly the problem a tagged union solves.
Unions vs Structs
A struct is a logical AND of its fields, all present together. A union is a logical OR: just one field lives at a time.
When to Reach for a Union
Use a union when a value naturally takes one of a few forms, like a token that is a number or a symbol, never both at once.
Quick Check
How many of a union's fields hold a value at the same time?
Recap
You met unions: one slot that holds a single field from several typed options. They share memory, so set and read only the active field. 🔀
Frequently asked questions
Is the “Unions That Hold One of Many Types” lesson free?
Yes — the full text of “Unions That Hold One of Many Types” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “Unions That Hold One of Many Types”?
Store alternative shapes in one slot. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Unions That Hold One of Many Types” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
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