How-To
Task-shaped recipes rather than a tour of the language. Databases: SQLite through its C API, the PostgreSQL wire protocol byte by byte, and a Redis RESP round trip. Concurrency with threads, atomics and a producer/consumer queue. SIMD scanning and dot products, zlib compression, binary wire formats, and memory layout. Every recipe is a complete program that CI compiled and ran. Sockets themselves are a section rather than a recipe: see Networking.
- Every recipe is a whole program, not a fragment. Copy the page and it builds.
- A recipe that will not run in your browser says so and says why: sockets, threads, a C library, or a real filesystem.
- Wire protocols are smaller than their client libraries suggest. PostgreSQL and RESP are each one file here, and both build on Networking.
24 chapters.
- Recipe: Word FrequenciesCount occurrences with a hash map, then rank them with a sort.
- Recipe: Loading a JSON ConfigDefaults for what's missing, validation for what's wrong, clean errors for the rest.
- Recipe: A Custom JSON SerializerGive a struct a jsonStringify method to rename fields and add computed ones.
- Recipe: A Binary Wire FormatSerialize a struct to explicit bytes and back, safely.
- Recipe: JSON vs Binary, by SizeThe same 100,000 records as JSON and as packed bytes, measured exactly.
- Recipe: A File-Backed To-Do StoreFixed-size records on disk, updated in place by id with positional reads and writes.
- Recipe: Compressing with zlibOne-shot compress and decompress through the system zlib, linked from Zig.
- Recipe: Catching Memory LeaksMake the test suite prove that every allocation is freed.
- Recipe: Printing Any StructComptime reflection builds a debug printer that works for every struct type.
- Recipe: Overflow Without PanicsHandling arithmetic that might not fit, without undefined behavior and without crashing.
- Recipe: The std.math ToolboxConstants, trig, powers, roots, rounding, and comparing floats safely.
- Recipe: A SIMD Dot ProductUsing @Vector to process four elements per operation, tail included.
- Recipe: SIMD Byte ScanningThe five-step pattern that turns a byte-at-a-time loop into a register-wide scan.
- Recipe: Vector AlgebraDot, length, normalize, and cross on @Vector, written like math instead of loops.
- Recipe: Keeping the Last N ItemsA generic fixed-capacity ring buffer built with a type function.
- Recipe: Set Operations on the CheapStaticBitSet turns membership, intersection, and union into single instructions.
- Recipe: Reproducible RandomnessSeeded generators make random worlds, tests, and replays repeatable.
- Recipe: Splitting Work Across ThreadsDivide a slice among threads with no locks and one join.
- Recipe: An Atomic Work IndexDynamic load balancing with fetchAdd instead of locks or chunking.
- Recipe: A Producer/Consumer Queuestd.Io.Queue as a bounded channel, with close() as the shutdown protocol.
- Recipe: Struct Memory LayoutRead the size, alignment, and field offsets a struct actually gets, and control them.
Databases
- Recipe: SQLite From ZigOpen a database, create a table, insert with a prepared statement, and query, over the C API.
- Recipe: A Redis RESP Round TripSpeak Redis's RESP protocol over a socket, with a tiny server and client in one process.
- Recipe: The PostgreSQL Wire ProtocolSpeak Postgres directly, framing a startup handshake and a query by hand.