Zig
Zig book notes exported from ChatGPT, organized into 24 chapters.
296 notes
Zig book notes exported from ChatGPT, organized into 24 chapters.
Zig is a programming language for writing fast, small, reliable programs.
The Zig compiler is not only a compiler for the Zig language. It is also the center of the Zig toolchain.
In this project, we will build a small command-line calculator.
Windows is one of Zig’s main supported platforms. You can write Zig programs on Windows, build Windows executables, call Windows system APIs, link with C libraries, and...
Zig has a formatting system built into the standard library. You have already used it many times through std.debug.print.
Performance is one of the main reasons people choose Zig.
A memory mapped file is a file that the operating system places into your program's address space.
A thread is a separate path of execution inside one program.
Zig works unusually well with C because it treats C as a first-class part of systems programming.
This chapter covered the builtins you will see most often as a beginner.
Zig has a built-in test system. You do not need a separate testing library to start writing tests.
@embedFile reads a file at compile time and embeds its bytes into the final program.
Logging means recording what a program is doing.
Zig has a built-in build system.
@compileError stops compilation with a custom error message.
Networking means communicating with another program through a network.
std is Zig’s standard library.
@panic stops the program immediately with a message.
An HTTP client is code that sends a request to a web server and reads the response.
An ArrayList is one of the most important data structures in Zig.
@floatCast converts one floating-point value to another floating-point type.
JSON is a text format for storing structured data.
You now know the core ideas behind Zig.
Memory is one of the most important ideas in Zig.
@truncate converts an integer to a smaller integer type by keeping only the low bits.
Compression means making data smaller.
Zig changes quickly before 1.0. That includes the build system.
Zig is still before 1.0, so the language and standard library can change between releases. Code written for one Zig version may not compile on another version without edits.
Zig has not reached 1.0 yet. The current release line discussed in this book is Zig 0.16, not Zig 1.16.
Zig has special built-in functions whose names start with @.
As of the current public Zig release line, the version is Zig 0.16, not Zig 1.16. Zig has not reached 1.0 yet. In this appendix, read “Zig 1.16” as “Zig 0.16” unless...
comptime is one of Zig’s strongest features, but it should not be used everywhere.
@intCast converts one integer value to another integer type.
An allocation-friendly API makes memory behavior clear to the caller.
Zig code should be explicit, simple, and easy to inspect. The goal is not cleverness. The goal is code that another programmer can read, verify, and maintain.
A data structure is never just a container.
A process is a running program.
Packaging means preparing your program so another person can download it, install it, and run it.
Benchmarking measures how fast code runs.
Wrapping a C library means building a Zig layer around it.
A shell is a program that reads commands and runs other programs.
High performance concurrent design means using several threads or tasks without making the program slower, more fragile, or harder to reason about.
Performance ideas become clearer when you see them inside real code.
Zig changes quickly because it is still moving toward 1.0.
IR means intermediate representation.
Git stores snapshots of files.
comptime means “compile time.”
Metaprogramming means writing code that helps create, inspect, or specialize other code.
@memcpy copies bytes from one memory region to another.
Memory lifetime means:
This appendix lists useful Zig projects to read after you know the basics.
Memory debugging means finding mistakes in how a program uses memory.
Contributing to Zig means helping the language, compiler, standard library, documentation, tests, or tooling improve.
A collection is a type that stores many values.
An environment variable is a named value provided to a program by the operating system.
A build does not only compile files. It can also install the results into a predictable output directory.
C headers are the bridge between Zig and C.
A CLI tool is a command-line program.
An abstraction is a way to hide detail behind a simpler interface.
A self-hosted compiler is a compiler written in the language it compiles.
Packaging means preparing your program so other people can download it, install it, run it, and trust what they are running.
A data race happens when two threads access the same memory at the same time, at least one access writes, and there is no proper synchronization.
A game engine is a program structure for running interactive simulations.
A memory leak happens when a program allocates memory and then loses the ability to free it.
Zig is designed to work closely with C. You can call C from Zig, call Zig from C, compile C code with Zig, and link Zig programs against existing C libraries.
A tree is a collection made of nodes.
GDB and LLDB are debuggers.
A programming language is never only its syntax.
Zig has built-in support for tests.
ABI means Application Binary Interface.
A TCP server is a program that waits for clients to connect.
Benchmarking means measuring how fast code runs.
A compiler translates one form of code into another form.
A cross-platform Zig program should not pretend that every operating system behaves the same way. Windows, Linux, macOS, WebAssembly, and embedded targets have different...
Random numbers are used when a program needs variation.
A queue is a data structure for passing work from one part of a program to another.
A bytecode VM is a small machine inside your program.
When Zig compiles a program, it can build the program in different modes.
A ring buffer is a fixed-size queue that reuses its storage.
An assertion is a check that must be true while the program runs.
LLVM is a compiler infrastructure project.
Zig can build the same program in different optimization modes.
Zig can call C, but C can also call Zig.
A network protocol is a rulebook for how programs talk over a network.
Branch prediction is a CPU optimization.
A virtual machine is a program that runs another program.
A portable API is an interface that works across more than one platform.
Programs often need to work with time.
An event loop is code that waits for events, then runs the right piece of work for each event.
A lexer is the first stage of many programming language tools.
A bit set is a compact collection of yes-or-no values.
A stack trace shows how your program reached a failure.
Code generation is the compiler stage that turns analyzed program meaning into target code.
Linking is the step where the compiler connects your program with the code it depends on.
A mixed Zig and C project contains source files from both languages.
Endianness means the order used to store the bytes of a multi-byte value.
Copying data is sometimes necessary, but unnecessary copying is one of the easiest ways to waste time and memory.
A parser reads text and turns it into structure.
Cross-target debugging means debugging a program built for a different machine, operating system, or CPU architecture than the one you are sitting at.
Parsing means turning text into data.
await means: wait until an asynchronous operation has finished, then continue with its result.
A database stores data so it can be saved, searched, updated, and loaded again later.
Cross compilation means building a program for a different machine than the one you are using.
Importing a C header lets Zig understand a C API. Linking gives the final program the actual compiled code.
A binary file format stores data as bytes with a specific structure.
Allocations are one of the most common causes of slow programs.
A game engine core is the small central layer that runs the game.
ARM is a CPU architecture family used in phones, tablets, laptops, routers, Raspberry Pi boards, microcontrollers, servers, and many embedded devices. When you write Zig for...
Formatting means turning values into text.
Async code lets a program start an operation now and receive the result later.
A memory allocator is code that gives memory to the rest of a program.
A file lives inside a directory.
A condition variable lets one thread sleep until another thread says that something has changed.
A thread pool is a group of worker threads that wait for jobs.
Most memory in Zig is ordinary memory.
Memory is where a program keeps its data while it runs.
Memory safety means using memory only while it is valid, only through the right type, and only inside the allowed range.
A dangling pointer is a pointer that refers to memory that is no longer valid.
Efficient text processing means working with text without doing unnecessary allocation, copying, or decoding.
Data-oriented design means you organize a program around the data it processes.
Zig programs use memory in different places. The two most important places are the stack and the heap.
A dynamic string is text whose length is not fixed ahead of time.
A state machine is a simple way to describe a program that moves between fixed states.
Type reflection means asking questions about a type while Zig is compiling the program.
An optional type is a type that can hold either a value or no value.
A sentinel-terminated array is an array with a special value at the end.
Zig string data is usually stored as UTF-8 bytes.
A union stores one active field at a time.
Type coercion means Zig converts a value from one type to another when the conversion is safe and well-defined.
Static dispatch means the compiler decides which code to call before the program runs.
@bitCast reinterprets the bits of one value as another type.
A struct is a type that groups several values together.
Alignment is a rule about where a value may be placed in memory.
Zig does not have a separate built-in mutable String type.
A tagged union is a type that can store one value from several possible shapes.
An error union is a value that can contain either:
A generic data structure is a data structure that works with more than one element type.
@ptrCast converts one pointer type into another pointer type.
Allocation can fail.
An array is a group of values stored next to each other.
Pointer arithmetic means moving a pointer forward or backward through memory.
A string literal is text written directly in your source code.
An enum is a type whose value must be one item from a fixed list.
anytype means the function parameter can accept many different types.
Reflection means inspecting a type as data.
@typeInfo asks the compiler for structured information about a type.
A custom allocator is an allocator you design for a specific memory policy.
Zig gives you direct control over memory. That control is useful, but it also means you must follow clear rules.
A slice is a view into memory.
An optional pointer is a pointer that may have no value.
An anonymous struct is a struct type without a name.
An opaque type is a type whose internal structure is hidden.
Generating code at compile time means using Zig code to create specialized program behavior before the final executable is built.
@Type builds a type from compile-time type information.
The page allocator asks the operating system for memory directly.
Queues and stacks are two simple ways to organize a collection of items.
Zig’s standard library is imported with:
A debug build is a build made for finding mistakes.
Semantic analysis is the compiler stage that checks what a program means.
Most programming languages need a way to handle failure.
A slice is a view into a sequence of values.
A slice is a view into a sequence of values.
A normal Zig struct is designed for ordinary data modeling.
A vector is a fixed-size group of values of the same type.
Parsing is the part of the compiler that reads source code and turns it into structure.
A compile-time loop is a loop that runs while Zig is compiling your program.
@alignOf asks the Zig compiler for the required memory alignment of a type.
A fixed buffer allocator gives memory from a buffer you already own.
Fuzz testing means testing a program with many generated inputs.
A C struct groups several fields into one value.
A linked list is a collection where each item points to the next item.
One of Zig’s strengths is compiler diagnostics. Zig tries to explain problems precisely instead of silently accepting dangerous behavior.
Terminal programming means writing programs that interact with the command line as more than simple text output.
A build option is a value passed from the command line into build.zig.
Embedded development means writing software for small computers inside devices.
SIMD means Single Instruction, Multiple Data.
WebAssembly, often shortened to Wasm, is a portable binary instruction format. It lets you compile code once and run it inside different hosts, such as web browsers, servers,...
Functions are reusable blocks of code.
A many item pointer is a pointer that can move across several values of the same type.
A multidimensional array is an array whose elements are also arrays.
A struct field can have a default value.
When people talk about Zig compiler internals, they often mention stage2.
A pointer stores the address of a value in memory.
Inline branching means Zig chooses a branch during compilation, not during runtime.
@sizeOf asks the Zig compiler how many bytes a type needs in memory.
A table-driven test checks many input cases with one test loop.
A StringHashMap is a hash map where the key is a string.
An arena allocator is an allocator that frees many allocations at once.
Calling a C function from Zig has three parts.
const std = @import"std";
Systems programming means working directly with the operating system.
Modern CPUs are fast, but memory is much slower.
A plugin architecture lets a program be extended without rewriting the whole program.
An atomic operation is a small operation that can safely happen while several threads are running.
Writing a file means sending bytes from your program to the operating system so they can be stored on disk.
macOS is one of Zig’s main desktop targets. You can use Zig on macOS to write command-line tools, development utilities, servers, libraries, and cross-platform applications.
A Zig project can depend on other Zig packages.
A static file server is a program that reads files from a directory and sends them to a browser over HTTP.
Programs need to make choices.
A pointer is a value that stores the address of another value.
An array literal is the syntax you use to write array values directly in source code.
A struct method is a function that belongs to a struct.
Zig 0.16.0 was released on April 14, 2026. The release contains 8 months of work, with changes from 244 contributors across 1,183 commits. The largest themes are the new I/O...
Optional unwrapping means taking the value out of an optional.
In the previous section, you learned that Zig can execute code during compilation.
One of the first Zig builtins you will learn is @import.
The general purpose allocator is Zig’s standard allocator for ordinary heap allocation.
A HashMap is a data structure for storing values by key.
A Zig build is made from steps.
A unit test checks one small piece of code in isolation.
Reading a file means asking the operating system for bytes stored on disk.
@cImport is Zig’s built-in way to import C declarations from header files.
A mutex is a lock for shared data.
A system call is a request from your program to the operating system.
When a program feels slow, your first job is not optimization.
Reflection means a program can inspect information about types while the program is being compiled or running.
Linux is one of the most natural platforms for Zig. Many Zig programs are built, tested, and deployed on Linux because Linux is common in servers, containers, embedded...
When you first open the Zig source code repository, it can feel overwhelming.
A JSON parser reads JSON text and turns it into data your program can use.
A program stores values so it can use them later. In Zig, you store values with two main keywords:
Zig is a programming language for writing fast, small, reliable programs.
Zig is a programming language for writing fast, small, reliable programs.
A program stores values so it can use them later. In Zig, you store values with two main keywords:
Programs need to make choices.
Functions are reusable blocks of code.
Most programming languages need a way to handle failure.
Memory is where a program keeps its data while it runs.
An array is a group of values stored next to each other.
A struct is a type that groups several values together.
An optional type is a type that can hold either a value or no value.
comptime means “compile time.”
Zig has special built-in functions whose names start with @.
Memory is one of the most important ideas in Zig.
An ArrayList is one of the most important data structures in Zig.
std is Zig’s standard library.
Zig has a built-in build system.
Zig has a built-in test system. You do not need a separate testing library to start writing tests.
Zig works unusually well with C because it treats C as a first-class part of systems programming.
A thread is a separate path of execution inside one program.
A memory mapped file is a file that the operating system places into your program's address space.
Performance is one of the main reasons people choose Zig.
Zig has a formatting system built into the standard library. You have already used it many times through std.debug.print.
Windows is one of Zig’s main supported platforms. You can write Zig programs on Windows, build Windows executables, call Windows system APIs, link with C libraries, and...
In this project, we will build a small command-line calculator.
The Zig compiler is not only a compiler for the Zig language. It is also the center of the Zig toolchain.
Zig is a programming language for writing fast, small, reliable programs.
A keyword is a word that has special meaning in Zig.
Sooner or later, every Zig beginner meets the compiler.
Names are part of the program.
A generic function is a function that works with many types instead of only one type.
You can write Zig code in any text editor.
Comments are notes for humans who read the code.
A compile error means Zig refused to build your program.
An exported function is a function made visible outside the current Zig program.
Errors are for expected failures.
So far, we have used single-file programs.
A string is text.
Some parts of a program should never run.
A calling convention defines how functions communicate at the machine level.
Most Zig programs start with small error sets:
When beginners hear the word “compiler,” they often think of one job:
Initialization means giving a value to something when it is created.
Many programs need to clean something up after using it.
An inline function is a function where the compiler may place the function’s code directly at the call site instead of performing a normal function call.
An error API is the part of your function signature that tells callers how failure works.
In the previous section, we used this command:
In Zig, undefined means “this value has not been initialized.”
Zig has normal loops that run when the program runs.
An anonymous function is a function without a permanent name.
Propagating an error means passing it to the caller instead of handling it immediately.
Now we will write and run a complete Zig program.
Type inference means Zig can figure out a type from the value you write.
Loops repeat code. But sometimes you do not want a loop to finish in the normal way.
Functions are values.
errdefer is a cleanup tool.
Before writing larger Zig programs, you need to understand an important fact:
A boolean is a value that can be only one of two things:
Zig has blocks.
Recursion is a technique where a function calls itself.
catch handles an error at the place where it happens.
Before we write more Zig code, we need the Zig compiler.
Floating point numbers are numbers with fractional parts.
A while loop repeats while a condition is true.
Many functions need to produce more than one piece of information.
try is the most common way to handle errors in Zig.
Zig exists because low-level programming is still important, but the old tools have painful tradeoffs.
Integers are whole numbers.
Programs often need to repeat work.
Functions often need to produce results.
An error union type means:
Zig is a programming language for writing programs that are fast, clear, and close to the machine.
Every value in a Zig program has a type.
An if statement is good for general conditions:
Function parameters are the inputs of a function.
An error set is a group of possible error names.