Zig course

Zig Level 4: Structs, Enums & Unions

Structs, Enums & Unions is level 4 of 11 of the LoWisa Zig course. It has 3 lessons and 9 ideas. Below is every lesson and what it teaches, in one line each.

Updated 2026-11-25

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

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.

Learn Zig on your phone

The first level is free. LoWisa is on Google Play and the App Store.