brain

tamnd's digital brain — notes, problems, research

42734 notes

Building Generic Data Structures

A generic data structure is a data structure that works with more than one element type.

zigbook
Type Coercion

Type coercion means Zig converts a value from one type to another when the conversion is safe and well-defined.

zigbook
Modeling State Machines

A state machine is a simple way to describe a program that moves between fixed states.

zigbook
Building Dynamic Strings

A dynamic string is text whose length is not fixed ahead of time.

zigbook
Build Options

A build option is a value passed from the command line into build.zig.

zigbook
The Basic Difference

Queues and stacks are two simple ways to organize a collection of items.

zigbook
Page Allocator

The page allocator asks the operating system for memory directly.

zigbook
`@typeInfo`

@typeInfo asks the compiler for structured information about a type.

zigbook
Reflection with `@typeInfo`

Reflection means inspecting a type as data.

zigbook
Error Union Internals

An error union is a value that can contain either:

zigbook
Union Safety

A union stores one active field at a time.

zigbook
UTF-8 Processing

Zig string data is usually stored as UTF-8 bytes.

zigbook
Stack vs Heap

Zig programs use memory in different places. The two most important places are the stack and the heap.

zigbook
Terminal Programming

Terminal programming means writing programs that interact with the command line as more than simple text output.

zigbook
Build a Static File Server

A static file server is a program that reads files from a directory and sends them to a browser over HTTP.

zigbook
`@Type`

@Type builds a type from compile-time type information.

zigbook
Generating Code at Compile Time

Generating code at compile time means using Zig code to create specialized program behavior before the final executable is built.

zigbook
`anytype`

anytype means the function parameter can accept many different types.

zigbook
Tagged Unions

A tagged union is a type that can store one value from several possible shapes.

zigbook
Sentinel-Terminated Arrays

A sentinel-terminated array is an array with a special value at the end.

zigbook
Opaque Types

An opaque type is a type whose internal structure is hidden.

zigbook
Appendix C. Common Compiler Errors

One of Zig’s strengths is compiler diagnostics. Zig tries to explain problems precisely instead of silently accepting dangerous behavior.

zigbook
Mutable Strings

Zig does not have a separate built-in mutable String type.

zigbook
Enums

An enum is a type whose value must be one item from a fixed list.

zigbook
Why Linked Lists Exist

A linked list is a collection where each item points to the next item.

zigbook
Using C Structs

A C struct groups several fields into one value.

zigbook
Fuzz Testing

Fuzz testing means testing a program with many generated inputs.

zigbook
Fixed Buffer Allocator

A fixed buffer allocator gives memory from a buffer you already own.

zigbook
`@alignOf`

@alignOf asks the Zig compiler for the required memory alignment of a type.

zigbook
Compile-Time Loops

A compile-time loop is a loop that runs while Zig is compiling your program.

zigbook
How Parsing Works

Parsing is the part of the compiler that reads source code and turns it into structure.

zigbook
Adding Dependencies

A Zig project can depend on other Zig packages.

zigbook
macOS Support

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.

zigbook
Writing Files

Writing a file means sending bytes from your program to the operating system so they can be stored on disk.

zigbook
Atomics

An atomic operation is a small operation that can safely happen while several threads are running.

zigbook
Alignment

Alignment is a rule about where a value may be placed in memory.

zigbook
Panic and Crash Behavior

Errors are for expected failures.

zigbook
Plugin Architectures

A plugin architecture lets a program be extended without rewriting the whole program.

zigbook
The Basic Idea

Modern CPUs are fast, but memory is much slower.

zigbook
Working with the OS

Systems programming means working directly with the operating system.

zigbook
Custom Error Types

Most Zig programs start with small error sets:

zigbook
Zig Generics Use `comptime`

A generic function is a function that works with many types instead of only one type.

zigbook
Build a JSON Parser

A JSON parser reads JSON text and turns it into data your program can use.

zigbook
Appendix B. Zig Cheat Sheet

const std = @import"std";

zigbook
Vectors

A vector is a fixed-size group of values of the same type.

zigbook
Anonymous Structs

An anonymous struct is a struct type without a name.

zigbook
String Literals

A string literal is text written directly in your source code.

zigbook
Pointer Arithmetic

Pointer arithmetic means moving a pointer forward or backward through memory.

zigbook
Designing Error APIs

An error API is the part of your function signature that tells callers how failure works.

zigbook
What “Exported” Means

An exported function is a function made visible outside the current Zig program.

zigbook
Calling C Functions

Calling a C function from Zig has three parts.

zigbook
Arena Allocator

An arena allocator is an allocator that frees many allocations at once.

zigbook
Why StringHashMap Exists

A StringHashMap is a hash map where the key is a string.

zigbook
Table-Driven Tests

A table-driven test checks many input cases with one test loop.

zigbook
`@sizeOf`

@sizeOf asks the Zig compiler how many bytes a type needs in memory.

zigbook
Inline Branching

Inline branching means Zig chooses a branch during compilation, not during runtime.

zigbook
Optional Pointers

An optional pointer is a pointer that may have no value.

zigbook
Propagating Errors

Propagating an error means passing it to the caller instead of handling it immediately.

zigbook
A Mental Model

A calling convention defines how functions communicate at the machine level.

zigbook
Compile Errors as Safety Checks

A compile error means Zig refused to build your program.

zigbook
Slice Lifetimes

A slice is a view into memory.

zigbook
Nullable Pointers

A pointer stores the address of a value in memory.

zigbook
Packed Structs

A normal Zig struct is designed for ordinary data modeling.

zigbook
Understanding Stage2

When people talk about Zig compiler internals, they often mention stage2.

zigbook
A Simple Function

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.

zigbook
Unreachable Code

Some parts of a program should never run.

zigbook
Zig Source Tree

When you first open the Zig source code repository, it can feel overwhelming.

zigbook
Linux Support

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...

zigbook
Reflection Systems

Reflection means a program can inspect information about types while the program is being compiled or running.

zigbook
Why Profiling Matters

When a program feels slow, your first job is not optimization.

zigbook
System Calls

A system call is a request from your program to the operating system.

zigbook
Mutexes

A mutex is a lock for shared data.

zigbook
`@cImport`

@cImport is Zig’s built-in way to import C declarations from header files.

zigbook
Reading Files

Reading a file means asking the operating system for bytes stored on disk.

zigbook
Writing Unit Tests

A unit test checks one small piece of code in isolation.

zigbook
Creating Build Steps

A Zig build is made from steps.

zigbook
The Basic Idea

A HashMap is a data structure for storing values by key.

zigbook
General Purpose Allocator

The general purpose allocator is Zig’s standard allocator for ordinary heap allocation.

zigbook
`@import`

One of the first Zig builtins you will learn is @import.

zigbook
Compile-Time Variables

In the previous section, you learned that Zig can execute code during compilation.

zigbook
`errdefer`

errdefer is a cleanup tool.

zigbook
What Anonymous Functions Usually Mean

An anonymous function is a function without a permanent name.

zigbook
Defer and Cleanup

Many programs need to clean something up after using it.

zigbook
Optional Unwrapping

Optional unwrapping means taking the value out of an optional.

zigbook
Slices

A slice is a view into a sequence of values.

zigbook
Slices in Detail

A slice is a view into a sequence of values.

zigbook
Default Field Values

A struct field can have a default value.

zigbook
`catch`

catch handles an error at the place where it happens.

zigbook
A First Example

Functions are values.

zigbook
Inline Loops

Zig has normal loops that run when the program runs.

zigbook
Appendix A. Zig 0.16 New Features

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...

zigbook
Zig Compiler Architecture

The Zig compiler is not only a compiler for the Zig language. It is also the center of the Zig toolchain.

zigbook
Build a CLI Calculator

In this project, we will build a small command-line calculator.

zigbook
Windows Support

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...

zigbook
Custom Formatting

Zig has a formatting system built into the standard library. You have already used it many times through std.debug.print.

zigbook
What Actually Makes Programs Slow?

Performance is one of the main reasons people choose Zig.

zigbook
Memory Mapped Files

A memory mapped file is a file that the operating system places into your program's address space.

zigbook
Threads in Zig

A thread is a separate path of execution inside one program.

zigbook
Why Zig Works Well with C

Zig works unusually well with C because it treats C as a first-class part of systems programming.

zigbook
Zig Test Framework

Zig has a built-in test system. You do not need a separate testing library to start writing tests.

zigbook