Zig is a low-level systems language that aims to be a simpler, more explicit C. It has no hidden control flow and no hidden memory allocation: if code allocates, you see an allocator passed in. It also ships a build system and a C-compatible toolchain, so it is often used to build C projects and for cross-compilation.
Key concepts
- Explicit allocators: functions that need memory take an allocator, so every allocation is visible and swappable.
- Comptime: ordinary code run at compile time, replacing macros and most generics.
- Error unions: errors are values in the return type, propagated with try and handled with catch.
- Optionals: a value that may be absent is a distinct type, not a null pointer.
- C interoperability: C headers can be imported and C code compiled alongside Zig.
- Safety modes: checks for overflow and bounds in debug builds that can be removed for speed.
Vocabulary
- Slice: a pointer paired with a length, the usual way to pass arrays.
- Defer and errdefer: cleanup that runs at scope exit, or only when an error is returned.
- Cross-compilation: building for another operating system or CPU from the same machine.
- Build script: the build is itself a Zig program that describes the steps.
What to study, in order
- Basic syntax, types and control flow from the language's own documentation.
- Write a small command-line tool, passing allocators and handling errors with try.
- Build a dynamic array and a hash map yourself to understand allocators.
- Use comptime to write a generic function and a type-level check.
- Call a C library from Zig, then compile your program for a different target.