Skip to content

Zig is a systems language built around one promise: no hidden control flow. There is no operator overloading, no hidden allocation, no exceptions unwinding the stack behind your back. Every function that allocates takes an allocator as a parameter, so you can see the cost at the call site and swap the strategy (arena, pool, testing allocator that detects leaks) without changing the code. And comptime lets ordinary Zig run at compile time, which replaces templates, macros, and generics with one mechanism. The exercises in this chapter put both ideas to work.

Concepts from other sections (1)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

Note: Zig is an optional language here. None of the core books target it, so these exercises have no worked solutions yet and nothing in this chapter is built in CI. The briefs are kept because they are worth reading as design problems. To get a toolchain, run brew install zig; on a chezmoi-managed machine it comes from the install_alt_langs flag, which is off by default. Zig 0.16 removed async/await from the language, so exercise 06 as listed below needs redefining before it can be written.

Learning objectives

  • Describe Zig’s explicit-allocator memory model, its error unions, and its comptime approach to generics.
  • Explain why passing an allocator to every allocating function makes memory cost visible and testable.
  • Use defer and errdefer to release resources correctly on both the success and the error path.
  • Reason through the design of each exercise brief before writing code.

You should be comfortable with manual memory management at the level of the C chapter: what an allocation is, what alignment means, and why a pointer into a buffer can dangle. Familiarity with C helps throughout, since Zig interoperates with it directly.

Zig has no garbage collector and no global allocator. Allocators are values passed as parameters, nothing allocates behind your back, and defer runs cleanup when a scope exits.

The type system is strong and static. Generics are ordinary functions evaluated at compile time that take and return types, which is what comptime means. Optional types (?T) replace null pointers, and error unions (E!T) carry either an error or a value.

Errors are handled with try (propagate) and catch (handle), and the runtime records an error return trace showing where an error came from and every frame it passed through. errdefer runs cleanup only when the scope exits with an error, which is the clean answer to partial-construction failures. For concurrency there is std.Thread; the language has no runtime. Earlier versions had stackless async/await, but it was removed in 0.16. Builds are described in build.zig, a Zig program, and C interoperability works out of the box. The exercises were written against Zig 0.13 and later, which is pre-1.0 but has a stable core.

  • An allocator parameter on every function that allocates.
  • comptime for generics and compile-time code generation.
  • defer and errdefer for cleanup, like Go’s defer but aware of the error path.
  • Sentinel-terminated slices, such as [:0]u8 for C strings.
  • @import("std"): one namespace, with every import explicit.
  • Tagged unions for sum types.
  • No hidden control flow: no operator overloading and no hidden allocations.

The briefs are listed below, ordered by concept rather than by difficulty, so choose by the difficulty rating (1 is easiest, 5 hardest) rather than by number.

#ExerciseConceptsDifficulty (1-5)
01ArrayList (generic)Comptime generics, allocator interface2
03Memory arenaAllocator interface, page allocation, alignment3
04JSON parserTagged unions, recursive descent, error unions4
05Compile-time regexComptime string processing, state machine generation5
06Async file serverAsync frames, I/O ring, event loop4
07C library wrapper@cImport, sentinel slices, translate-c3
08SIMD vector math@Vector, platform intrinsics, benchmarking4
09Build system pluginbuild.zig, custom steps, dependency graph3
10Toy ELF linkerBinary formats, memory layout, relocations5

There is no exercise 02. It was a hash map, and that problem is now covered in full by A Swiss table and A Robin Hood hash map in the Systems data structures chapter of Algorithms. For exercise 03, the C arena allocator is a worked version of the same idea.

  • Explicit allocator: the Zig convention that every function that allocates takes an allocator as a parameter, so memory cost is visible at the call site and the strategy (arena, pool, leak-detecting test allocator) can change without changing the code.
  • Comptime / compile-time execution: Zig’s mechanism for running ordinary Zig code during compilation; generics are functions that take and return types at compile time, replacing templates, macros, and separate generics syntax.
  • Error union: a Zig type E!T that holds either an error from the set E or a value of type T; try propagates the error and catch handles it.
  • errdefer: a Zig statement that schedules cleanup to run only if the enclosing scope exits with an error, which undoes a partial construction without touching the success path.
  • Error return trace: a record, kept by the Zig runtime, of where an error was created and every frame it was returned through, so a failure can be traced without exceptions or stack unwinding.
  • No hidden control flow: Zig’s design rule that code does only what it visibly says: no operator overloading, no hidden allocations, and no exceptions, so every call, allocation, and early return appears in the source.