Standard Library
This section shows what std contains, with a working program for each part. The first parts are allocators and how to find a leak, ArrayList and the hash maps, and JSON in both directions. The section also shows the Io interface with its readers and writers, crypto, time, Unicode and the filesystem. std changes faster than the language. When you get a new compiler, read these pages again first.
- Allocation is always visible. A function that needs memory takes an
Allocator, so you can always see which functions allocate. - Blocking is also always visible. A function that can block takes an
std.Io, and the caller decides if theIouses threads. std.testing.allocatorfails a test that leaks memory. Thus a leak fails the build here, and a user does not find it later in production.- std changes faster than the language. When code does not compile after an upgrade, look here first.
26 chapters.
- The Io InterfaceEach operation that can block takes an Io. The caller selects how the Io works.
- AllocatorsExplicit memory management, with the allocator given as a value.
- ArrayListThe array that can grow, 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.
- MultiArrayListA struct of arrays, with a memory layout that helps the cache.
- Bit Sets and Enum CollectionsA set of small integers in a word, and containers with an enum as the key.
- StringsSearch, trim, split, join. All of these functions are in std.mem.
- UnicodeA string is bytes until you select UTF-8 support.
- FormattingFormat strings that the compiler checks.
- Advanced FormattingCustom format methods, and the format specifiers that changed.
- Parsing and EncodingText to numbers, and bytes to hex or base64.
- String RecipesPadding, runtime concatenation, and the std.mem and std.ascii calls that the Strings chapter does not show.
- Unicode RecipesTruncate on a character boundary, decode one codepoint, and convert to UTF-16.
- Readers and WritersThe buffered Reader and Writer interfaces introduced in Zig 0.15.
- JSONParse JSON into real types, and write the types back as JSON.
- FilesystemEach disk operation now takes an Io.
- Command-Line ArgumentsHow to read argv and the environment through std.process.
- SortingIn-place sorts with a comparator that you supply.
- IteratorsA convention, not an interface.
- Random NumbersSeeded generators that you give to your code as arguments.
- CryptoHashes, MACs, and constant-time comparison.
- TimeThe clocks are behind the Io interface. The constants and calendars are in std.time.
- LoggingOne log function, selected at the root and known at compile time.
- TestingThe assertions other than expect, and the allocator that finds leaks.