Structs
- Structs and methods. A struct holds fields and declares functions: methods as namespaced fns taking self.
- Defaults and init. Fields take default values; init functions build valid instances: construction explicit.
- Anonymous structs. Anonymous struct literals pass options: .{ .retries = 3 }: named arguments by shape.
Enums
- Enums. An enum names a closed set: switch over it is exhaustive, checked forever.
- Tagged values. Enums carry integer tags when needed: @intFromEnum crossing to numbers explicitly.
- Non-exhaustive enums. An enum can be open (_) for forward compatibility: the escape declared, not accidental.
Tagged Unions
- Tagged unions. A union(enum) stores one variant plus its tag: Zig’s sum type, switch-destructured.
- Payload capture. switch captures the payload: case .circle => |radius|: data bound as it matches.
- Modelling with unions. Tagged unions model states honestly: a Message that cannot be two things at once.
Before and after this level
- Before: Level 3: Allocators: Explicit Allocation, Allocator Interfaces, Designing Memory Ownership.
- Next: Level 5: Errors & Optionals: Error Unions, Optionals, Explicit Failure Handling.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does Zig Level 4: Structs, Enums & Unions cover?
Structs, Enums, Tagged Unions: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 3: Allocators, which covers Explicit Allocation, Allocator Interfaces, Designing Memory Ownership.