Standard Library Overview
Zig’s standard library (std) provides a comprehensive set of modules for building robust, portable applications. The standard library is designed to be minimal, explicit, and platform-agnostic while providing powerful abstractions for common programming tasks.
Importing the Standard Library
All Zig programs import the standard library through a consistent pattern:std namespace:
Library Organization
The standard library is organized into logical modules, each serving a specific domain. Here’s how the library is structured:Core Modules
mem
Memory management, allocators, and memory utilities
heap
Heap allocator implementations (Arena, Page, Debug)
fs
File system operations and directory management
os
Operating system interfaces and syscalls
Data Structures
ArrayList
Dynamic, growable arrays
HashMap
Hash-based key-value storage
ArrayHashMap
Iteration-order-preserving hash maps
LinkedList
Singly and doubly linked lists
Format and Parsing
fmt
String formatting and parsing
json
JSON parsing and stringification (RFC 8259)
base64
Base64 encoding and decoding
unicode
Unicode utilities and UTF-8/UTF-16 handling
Main Exports from std.zig
The mainstd.zig file re-exports commonly used types and modules:
lib/std/std.zig
Many types have both managed and unmanaged variants (e.g.,
HashMap and HashMapUnmanaged). Unmanaged versions don’t store the allocator, requiring it to be passed to each method.Standard Library Options
You can customize standard library behavior viastd_options in your root file:
root.zig
Available Options
lib/std/std.zig:114-176
Module Categories
Memory Management
std.mem- Memory utilities, comparison, and the Allocator interfacestd.heap- Heap allocator implementations
System Interaction
std.os- Low-level operating system APIsstd.posix- POSIX-compliant system callsstd.fs- File system operationsstd.process- Process management and environment variables
Data Structures & Algorithms
std.ArrayList- Dynamic arraysstd.HashMap,std.ArrayHashMap- Hash mapsstd.sort- Sorting algorithmsstd.PriorityQueue- Priority queue implementation
Text Processing
std.fmt- String formatting and parsingstd.ascii- ASCII character utilitiesstd.unicode- Unicode operationsstd.json- JSON parsing and serialization
Cryptography & Hashing
std.crypto- Cryptographic primitivesstd.hash- Hash functionsstd.Random- Random number generation
Debugging & Testing
std.debug- Debug utilities and stack tracesstd.testing- Testing framework and assertionsstd.log- Structured logging
Networking & I/O
std.http- HTTP client implementationstd.Io- I/O abstractions (Reader, Writer)std.net- Network operations
Build System
std.Build- Build system APIstd.Target- Target platform informationstd.SemanticVersion- Semantic versioning
Common Patterns
Using Allocators
Most standard library types require an allocator:Error Handling
Zig uses explicit error handling throughout the standard library:Testing
The standard library provides a comprehensive testing framework:Platform Support
The standard library is designed to work across all Zig-supported platforms:- Desktop: Linux, Windows, macOS
- Mobile: iOS, Android (via Zig’s cross-compilation)
- Embedded: Freestanding, various microcontrollers
- Web: WebAssembly (WASI)
Platform-specific code is isolated behind consistent interfaces. The standard library uses compile-time introspection (
@import("builtin")) to adapt to the target platform.Next Steps
Allocators
Deep dive into Zig’s memory allocator system
Testing Framework
Learn about testing and assertions
Key Modules
Explore the most important standard library modules
Build System
Understand the Zig build system