⚡ Zig Guide LiveUnofficialbut fully verified
✓ Zig 0.17.0-dev.1503+1f1bee62eOn an older Zig?

Standard Library

What ships in std, with a working program for each piece: allocators and how to catch a leak, ArrayList and the hash maps, JSON in both directions, the Io interface with its readers and writers, threads, crypto, time, Unicode and the filesystem. std moves faster than the language, so these are the pages most worth re-reading against a fresh compiler.

  1. Nothing allocates behind your back. A function that needs memory takes an Allocator, so you can always see what will.
  2. Nothing blocks behind your back either. A function that can block takes an std.Io, and the caller decides whether that means threads.
  3. std.testing.allocator fails a test that leaks. A leak is a red build here, not something found in production later.
  4. std moves faster than the language does. When something stops compiling after an upgrade, look here first.

31 chapters.

  • The Io InterfaceAnything that can block takes an Io; the caller picks how.
  • AllocatorsExplicit memory management, passed as a value.
  • ArrayListThe growable array, now unmanaged.
  • Hash MapsKey-value storage, and the getOrPut pattern.
  • StacksArrayList from one end, or no allocator at all.
  • PriorityQueueA binary heap where the comparator is part of the type.
  • MultiArrayListStruct of arrays, laid out for the cache.
  • Bit Sets and Enum CollectionsA set of small integers in a word, and containers keyed by enums.
  • StringsSearch, trim, split, join. All of it lives in std.mem.
  • UnicodeA string is bytes until you opt in to UTF-8.
  • FormattingFormat strings checked at compile time.
  • Advanced FormattingCustom format methods, and the specifiers that changed.
  • Parsing and EncodingText to numbers, and bytes to hex or base64.
  • Readers and WritersThe post-writergate I/O interfaces.
  • JSONParsing into real types, and back out again.
  • FilesystemEvery disk operation now takes an Io.
  • Command-Line ArgumentsReading argv and the environment through std.process.
  • SortingIn-place sorts with an explicit comparator.
  • IteratorsA convention, not an interface.
  • Random NumbersSeeded generators, passed explicitly.
  • CryptoHashes, MACs, and constant-time comparison.
  • TimeClocks behind the Io interface; constants and calendars in std.time.
  • LoggingOne log function, chosen at the root and known at compile time.
  • ThreadsReal OS threads, and why this page cannot run in your browser.
  • Concurrencyasync, Future, and Group, all through the Io interface.
  • CancellationStopping a running task, and the cancellation points that make it possible.
  • QueuesIo.Queue as a bounded channel between tasks, with close and back pressure.
  • SelectWaiting for whichever task finishes first, and cancelling the rest.
  • Locks and SemaphoresRwLock, Semaphore and Condition, past the point where a Mutex is enough.
  • Choosing an IoThreaded, the evented implementations, and building one yourself.
  • TestingThe assertions past expect, and the allocator that catches leaks.