Home / Zig
Zig
Zig at intermediate depth: allocators and ownership, error traces, comptime patterns, packed and extern types, threads and pools, testing depth, build.zig and cross-compilation, C interop, and four projects. Every example is verified with the real compiler.
Zig stops feeling small once you manage memory on purpose, bend the compiler to your will at compile time, and run work on more than one thread. This course takes the basics you already know and turns them into working habits: allocators chosen for the job, errors carried cleanly, types shaped to the bit, and projects built with the real build tool.
Course contents
Work top to bottom — the sidebar is the course. Topics later in the list assume the ones before them.
Zig Intermediate Introduction
Intermediate · ⏱ 3 min · standalone lesson
Memory in Depth
- Zig Arena Patterns
- Zig General Purpose Allocator Patterns
- Zig Fixed Buffer Allocator
- Zig Allocation Errors
- Zig Custom Allocator
- Zig Ownership Patterns
Error Handling in Depth
- Zig Error Sets in Depth
- Zig Error Union Patterns
- Zig Errdefer Patterns
- Zig Error Traces
- Zig Anyerror Patterns
Comptime in Depth
- Zig Type Introspection
- Zig Generic Data Structures
- Zig Comptime Functions
- Zig Comptime Branches
- Zig Comptime Strings
- Zig Comptime Patterns
Types in Depth
- Zig Packed Structs
- Zig Tagged Unions in Depth
- Zig Struct Methods in Depth
- Zig Union Safety
- Zig Extern Types
Concurrency
Start: Zig Thread Basics ❯Testing in Depth
- Zig Test Blocks in Depth
- Zig Test Assertions
- Zig Error Test Cases
- Zig Test Helpers
- Zig Test Organization
Build System
- Zig Build Scripts in Depth
- Zig Build Options
- Zig Build Dependencies
- Zig Build Tests
- Zig Cross-Compilation
C Interop
Start: Zig C Imports ❯Projects
- Zig Project: Generic Library
- Zig Project: Command-Line Tool
- Zig Project: Config Parser
- Zig Project: Task Scheduler
Take Zig further
The beginner course taught Zig as plain programs: one file, one main function, and output you could read beside the code. This intermediate course keeps that shape and raises the stakes. Programs now own resources, recover from failures, share work between threads, and ship through build scripts, so every lesson answers a question a real Zig program has to face.
Memory is the first deep water. You will run arenas that throw everything away at once, a general purpose allocator that watches for leaks, a fixed buffer that never touches the heap, and a counting allocator you write yourself, then settle who owns each allocation and who frees it. Nothing here is theory: every allocator lesson allocates, measures, and cleans up in front of you.
Comptime is the second deep water, and it is where Zig stops looking like other languages. You will inspect types while compiling, generate data structures for any element type, branch on what a type declares, and assemble strings before the program ever runs. The patterns chapter then folds those tricks into reusable shapes you can reach for again.
The rest of the course is about programs that live in the world: threads that share counters safely, tests that pin down failure paths, a build script with options and dependencies, cross-compiled binaries for machines you do not own, and calls into C libraries. Four closing projects tie it together, from a generic library to a task scheduler.
What you will learn
The lessons move from memory to projects in a fixed order, each one leaning on the ones before it. By the final project, you can do all of the following:
- Pick an allocator for the job: reset an arena in one call, read a general purpose allocator's leak report, run on a fixed buffer when the heap is off limits, and wrap any allocator in a counting layer that proves what it spent.
- Give every allocation an owner with a deinit method, so freeing happens in exactly one place and double frees stop being a worry.
- Name errors precisely with error sets, carry them through error unions, clean up on the failing path with errdefer, read the return trace to see how deep a failure travelled, and decide when anyerror is honest and when it hides too much.
- Inspect a type at compile time, build a generic stack that works for integers and text alike, run functions during compilation, branch on declarations a type carries, and assemble strings and tables before the program starts.
- Pack fields to the bit, read a tagged union only through its active field, write methods that mutate through a pointer self, and describe C-shaped memory with extern structs and unions.
- Start threads and wait for them in order, share a counter through atomics, guard shared state with a mutex, and drain a thread pool before printing anything it produced.
- Write test blocks that pin down success and failure alike, share setup through helper functions, and organise tests so a failure names the exact behaviour that broke.
- Drive a project with build.zig: add options, pull in a local dependency, wire a test step, and compile for another operating system and processor without leaving your machine.
- Call C functions two ways, read C structs like div_t, and allocate C memory with malloc and calloc while freeing it on every path.
- Finish with four complete programs: a generic library with tests, a command-line tool that parses flags, a config parser that turns lines into settings, and a task scheduler that runs steps in order and names the one that failed.
Who this Zig course is for
This course assumes you finished the Zig beginner course or arrived with the same ground: variables and types, control flow, functions, structs, error sets, pointers, allocators at a first look, and comptime generics at a first look. New words are still defined where they first appear, but the pace assumes the basics are already yours.
It suits programmers who want systems depth without switching languages. If you write tools, servers, games, or embedded code and want to understand what your program costs in memory and threads, these lessons give you that understanding in Zig's own terms.
The 46 lessons add up to 172 minutes of reading and practice, close to three hours. A steady learner covers that in a handful of sittings, then treats the four projects as the real test of whether the ideas stuck.
How this course works
The order is fixed on purpose: memory first, then errors, comptime, types, threads, tests, builds, C, and finally projects. A later page trusts the earlier ones, so skipping ahead will leave you guessing at words the course already defined.
All 184 code blocks passed through the real Zig 0.14.1 compiler twice, and the output shown next to each one is the output those runs produced. Build chapters are no exception: their outputs come from genuine build runs, including cross-compiled binaries that were built, not executed, on this machine.
There is no web playground for Zig that this site could honestly embed, so nothing here pretends to execute code in your browser. You read real code beside real output, and the build and test chapters give you the commands to repeat every run locally.
A short exercise ends each lesson, with the answer explained rather than just marked. Progress lives in your browser, so an interrupted evening resumes at the next unfinished lesson without any account or sign-in.
Frequently asked questions
Is this Zig intermediate course free?
Yes, fully. You can read all 46 lessons, try all the exercises, and study all four projects without paying anything or creating an account.
What should I know before starting this Zig intermediate course?
You should be comfortable with the beginner course: printing, variables, types, control flow, functions, structs, error sets, pointers, basic allocators, and a first look at comptime. This course does not re-teach those ideas; it builds on them from the first memory lesson.
Do I need to install Zig to follow along?
Not to learn from these pages: every program is printed next to the output it truly produced, so the ideas land without a toolchain. When you want hands-on practice, Zig installs as one download from ziglang.org, and the build chapters walk through multi-file projects command by command.
How long does this Zig intermediate course take to finish?
Added together, the 46 lessons hold 172 minutes of material, just under three hours. A steady pace covers that in a handful of sittings, with the four projects kept as open-ended practice afterwards.
Does this course cover threads and parallel programming?
Yes, at a careful first depth: spawning and joining threads, atomic counters, mutexes, and a thread pool that is drained before anything is printed. Every concurrent example is written so its output is the same on every run, which is the discipline real multithreaded code needs.
What should I learn after this Zig intermediate course?
Rust Intermediate is the nearest neighbour: 149 lessons on generics, traits and iterators in a language whose compiler polices ownership itself, which throws Zig's do-it-yourself approach into sharp relief. Go Intermediate and C++ Intermediate are the other sensible continuations, and the blurbs below say why.
Next steps
- Rust Intermediate ❯ — Rust Intermediate spans 149 lessons over generics, traits and iterators, and its compiler polices ownership where Zig leaves that job to you, a difference that sharpens both languages.
- Go Intermediate ❯ — Go Intermediate runs 34 lessons through interfaces, generics and channels, handy if you want concurrent services with less ceremony once Zig's explicit threading feels familiar.
- C++ Intermediate ❯ — C++ Intermediate covers 34 lessons on ownership, copies and moves in the venerable language Zig set out to improve, so Zig's allocator choices read more clearly with both in view.