How-To
Each recipe does one task, and the recipes are not a tour of the language. The database recipes are SQLite through its C API, the PostgreSQL wire protocol byte by byte, and a Redis RESP round trip. The concurrency recipes use threads, atomics and a producer/consumer queue. Other recipes show SIMD scans and dot products, zlib compression, binary wire formats and memory layout. CI compiled and ran each recipe as a complete program. Sockets have their own section, Networking, and are not a recipe.
- Every recipe is a full program, not a fragment. If you copy the program from the page, it builds.
- If a recipe cannot run in your browser, the recipe tells you 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 use the Networking chapters as a base.
24 chapters.
- Recipe: Word FrequenciesCount occurrences with a hash map, then rank them with a sort.
- Recipe: Load a JSON ConfigUse defaults for absent fields, validate wrong values, and return clear errors for the other problems.
- Recipe: A Custom JSON SerializerGive a struct a jsonStringify method to rename fields and add computed fields.
- Recipe: A Binary Wire FormatSerialize a struct to explicit bytes and back, with safe checks.
- Recipe: JSON vs Binary, by SizeThe same 100,000 records as JSON and as packed bytes, with exact measurements.
- Recipe: A File-Backed To-Do StoreRecords of fixed size on disk, updated in place by id with positional reads and writes.
- Recipe: Compress with zlibCompress and decompress in one call each through the system zlib, linked from Zig.
- Recipe: Catch Memory LeaksMake the test suite prove that the code frees each allocation.
- Recipe: Print Any StructComptime reflection builds a debug printer that works for all struct types.
- Recipe: Overflow Without PanicsDo arithmetic on values that can be too large, without undefined behavior and without a crash.
- Recipe: The std.math ToolboxConstants, trig, powers, roots, rounding, and a safe comparison of floats.
- Recipe: A SIMD Dot ProductUse @Vector to process four elements in each operation, with the tail too.
- Recipe: Scan Bytes with SIMDThe five-step pattern that changes a loop of one byte at a time into a scan as wide as a register.
- Recipe: Vector AlgebraDot, length, normalize and cross on @Vector, written as math and not as loops.
- Recipe: Keep the Last N ItemsA generic ring buffer with a fixed capacity, built with a type function.
- Recipe: Set Operations on the CheapA static bit set does membership, intersection and union with one instruction each.
- Recipe: Reproducible RandomnessSeeded generators make random worlds, tests and replays repeatable.
- Recipe: Split Work Across ThreadsDivide a slice among threads with no locks and one join.
- Recipe: An Atomic Work IndexDynamic load balancing with fetchAdd, without locks or chunks.
- 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 that a struct really gets, and control them.
Databases
- Recipe: SQLite From ZigOpen a database, create a table, insert with a prepared statement, and query, through the C API.
- Recipe: A Redis RESP Round TripUse the RESP protocol of Redis over a socket, with a small server and client in one process.
- Recipe: The PostgreSQL Wire ProtocolUse the Postgres protocol directly, and frame a startup handshake and a query by hand.