Skip to content

Appendix D. Important Standard Library APIs

Zig’s standard library is imported with:

Zig’s standard library is imported with:

const std = @import("std");

Most useful standard library APIs live under std.

This appendix is a beginner map. It does not cover every function. It shows the parts you will use most often.

D.1 Printing

Use std.debug.print for simple output while learning.

const std = @import("std");

pub fn main() void {
    std.debug.print("Hello\n", .{});
}

With values:

std.debug.print("x = {}\n", .{x});

With strings:

std.debug.print("name = {s}\n", .{name});

Common format patterns:

PatternMeaning
{}Default formatting
{s}String formatting
{any}Debug-style formatting

D.2 Testing

Use std.testing for unit tests.

const std = @import("std");

fn add(a: i32, b: i32) i32 {
    return a + b;
}

test "add works" {
    try std.testing.expect(add(2, 3) == 5);
}

Run:

zig test main.zig

Useful testing APIs:

APIUse
std.testing.expectAssert that a condition is true
std.testing.expectEqualCompare expected and actual values
std.testing.expectErrorCheck that an error happens
std.testing.allocatorAllocator for tests

D.3 Memory Allocation

Zig does not hide heap allocation. Many APIs ask for an allocator.

Common allocator type:

std.mem.Allocator

Page allocator:

const allocator = std.heap.page_allocator;

General-purpose allocator pattern:

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();

const allocator = gpa.allocator();

Arena allocator pattern:

var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();

const allocator = arena.allocator();

Allocate bytes:

const buf = try allocator.alloc(u8, 1024);
defer allocator.free(buf);

Important APIs:

APIUse
allocator.alloc(T, n)Allocate n items of type T
allocator.free(slice)Free allocated slice
allocator.create(T)Allocate one item
allocator.destroy(ptr)Free one item
std.heap.page_allocatorSimple allocator backed by OS pages
std.heap.GeneralPurposeAllocatorDebug-friendly general allocator
std.heap.ArenaAllocatorMany allocations, one cleanup

D.4 Memory Utilities

std.mem contains functions for working with slices, bytes, and memory.

Compare slices:

if (std.mem.eql(u8, a, b)) {
    // same bytes
}

Copy bytes:

@memcpy(dest, source);

Fill bytes:

@memset(buffer, 0);

Find value:

const index = std.mem.indexOf(u8, text, "needle");

Useful APIs:

APIUse
std.mem.eqlCompare slices
std.mem.indexOfFind subslice
std.mem.startsWithCheck prefix
std.mem.endsWithCheck suffix
std.mem.trimTrim characters
std.mem.splitScalarSplit by one byte
std.mem.tokenizeScalarTokenize by one byte

D.5 ArrayList

std.ArrayList is a growable array.

var list = std.ArrayList(i32).init(allocator);
defer list.deinit();

try list.append(10);
try list.append(20);

Access items:

for (list.items) |item| {
    std.debug.print("{}\n", .{item});
}

Useful APIs:

APIUse
appendAdd one item
appendSliceAdd many items
popRemove last item
clearRetainingCapacityClear but keep memory
clearAndFreeClear and free memory
itemsSlice of stored items

D.6 Hash Maps

String keys:

var map = std.StringHashMap(i32).init(allocator);
defer map.deinit();

try map.put("one", 1);
try map.put("two", 2);

Get a value:

if (map.get("one")) |value| {
    std.debug.print("{}\n", .{value});
}

Generic hash map:

std.AutoHashMap(K, V)

Useful APIs:

APIUse
putInsert or replace
getRead value
containsCheck key
removeRemove key
iteratorIterate entries
countNumber of entries

D.7 Files

Open a file:

const file = try std.fs.cwd().openFile("data.txt", .{});
defer file.close();

Create a file:

const file = try std.fs.cwd().createFile("out.txt", .{});
defer file.close();

Read and write APIs vary by Zig version. In Zig 0.16, newer I/O APIs use the std.Io model. The main idea remains the same: open files through std.fs, then read or write through the appropriate reader or writer API.

Directory APIs:

APIUse
std.fs.cwd()Current working directory
openFileOpen existing file
createFileCreate or truncate file
makeDirCreate directory
deleteFileDelete file
openDirOpen directory
closeClose file or directory handle

D.8 Paths

Use std.fs.path for path operations.

const base = std.fs.path.basename("/tmp/file.txt");

Useful APIs:

APIUse
basenameLast path component
dirnameParent path
extensionFile extension
joinJoin path parts
isAbsoluteCheck absolute path

Path handling is platform-sensitive. Avoid manual string concatenation for paths.

D.9 Formatting

Use std.fmt for converting values to text.

Format into a buffer:

var buf: [64]u8 = undefined;

const text = try std.fmt.bufPrint(&buf, "x = {}", .{x});

Parse integer:

const n = try std.fmt.parseInt(i32, "123", 10);

Parse float:

const f = try std.fmt.parseFloat(f64, "3.14");

Useful APIs:

APIUse
bufPrintFormat into fixed buffer
allocPrintFormat into allocated string
parseIntParse integer
parseFloatParse float
formatCustom formatting support

D.10 Math

Use std.math.

const x = std.math.sqrt(16.0);

Common APIs:

APIUse
sqrtSquare root
sin, cos, tanTrigonometry
log, log2, log10Logarithms
min, maxMinimum and maximum
clampClamp value into range
absIntAbsolute value for integers

Some operations may return errors or require specific types. Read the function signature.

D.11 Random Numbers

Use std.Random.

A common pattern:

var prng = std.Random.DefaultPrng.init(12345);
const random = prng.random();

const n = random.int(u32);

For serious security-sensitive randomness, use a cryptographic random source, not a deterministic PRNG.

Useful APIs:

APIUse
random.int(T)Random integer
random.float(T)Random float
random.boolean()Random bool
random.bytes(buf)Fill buffer with random bytes

D.12 Time

Use std.time.

const start = std.time.nanoTimestamp();

// work

const end = std.time.nanoTimestamp();
const elapsed = end - start;

Useful APIs:

APIUse
nanoTimestampCurrent timestamp in nanoseconds
milliTimestampCurrent timestamp in milliseconds
sleepSleep for a duration
ns_per_sNanoseconds per second
ms_per_sMilliseconds per second

D.13 Process

Use std.process for process-level data and child processes.

Common needs:

API areaUse
ArgumentsRead command-line arguments
EnvironmentRead environment variables
Child processRun another program
Exit codeControl process result

In Zig 0.16, process arguments and environment handling are less global and fit better with explicit I/O design. Expect examples to pass context more explicitly than older Zig code.

D.14 Sorting

Use std.sort.

Example idea:

std.sort.heap(i32, items, {}, lessThan);

Comparator:

fn lessThan(_: void, a: i32, b: i32) bool {
    return a < b;
}

Sorting APIs may vary by version, so check the exact signature for your Zig release.

D.15 JSON

Use std.json.

Common operations:

TaskAPI area
Parse JSONstd.json.parseFromSlice
Build JSON textstd.json.stringify
Read dynamic JSONstd.json.Value
Parse into structtyped parsing APIs

Example type:

const User = struct {
    name: []const u8,
    age: u32,
};

JSON APIs often need an allocator because parsed data may allocate memory.

D.16 HTTP

Zig’s standard library includes HTTP support, but HTTP APIs have changed across releases.

For beginner projects, learn these ideas first:

ConceptMeaning
ClientSends HTTP requests
ServerReceives HTTP requests
HeadersMetadata such as content type
BodyRequest or response data
MethodGET, POST, PUT, etc.
StatusResponse code such as 200 or 404

When using Zig 0.16, prefer examples written for Zig 0.16 because older HTTP examples may not compile unchanged.

D.17 Logging

Use std.log.

std.log.info("server started on port {}", .{port});

Levels:

LevelUse
debugDetailed debugging
infoNormal information
warnSuspicious but recoverable situation
errError condition

D.18 Threading

Use std.Thread.

Example shape:

const thread = try std.Thread.spawn(.{}, worker, .{});
thread.join();

Worker:

fn worker() void {
    // work here
}

Related APIs:

APIUse
std.Thread.spawnStart a thread
joinWait for thread
std.Thread.MutexMutual exclusion
std.Thread.ConditionWait and signal
std.atomicAtomic operations

D.19 Networking

Use std.net for networking.

Common concepts:

ConceptMeaning
AddressIP address plus port
TCPReliable byte stream
UDPDatagram protocol
ListenerWaits for incoming connections
StreamRead/write connection

Networking code usually involves I/O, errors, buffers, and careful resource cleanup.

D.20 Build System

The build system uses build.zig.

Common names:

APIUse
std.BuildBuild system root type
addExecutableAdd executable target
addTestAdd test target
addRunArtifactRun a build artifact
standardTargetOptionsTarget selection
standardOptimizeOptionDebug/release mode

A small build script creates an executable, sets its root source file, and installs it.

D.21 C Interop

Zig can import C headers:

const c = @cImport({
    @cInclude("stdio.h");
});

Then call C functions:

_ = c.printf("hello\n");

Zig can also compile C code through:

zig cc

This is one of Zig’s practical strengths.

D.22 Common Import Map

ImportUse
std.debugDebug printing
std.testingTests
std.memMemory and slice utilities
std.heapAllocators
std.fsFilesystem
std.fs.pathPath handling
std.fmtFormatting and parsing
std.mathMath helpers
std.timeTime
std.processProcess APIs
std.jsonJSON
std.logLogging
std.ThreadThreads
std.netNetworking
std.BuildBuild system

D.23 How to Read Standard Library Code

The standard library is ordinary Zig code.

When documentation is unclear, open the source.

Look for:

What to checkWhy it matters
Function signatureShows arguments, errors, and return type
Allocator parameterMeans the function may allocate
Error unionMeans the function can fail
comptime parameterMeans value is known at compile time
defer usageShows cleanup rules
TestsShow intended behavior

A useful habit is to read signatures before reading implementation.

Example:

pub fn parseInt(comptime T: type, buf: []const u8, radix: u8) !T

This tells you a lot:

T is known at compile time.

buf is the input text.

radix is the base, such as 10 or 16.

The function may fail.

The successful result has type T.

D.24 What to Memorize First

For beginners, memorize these first:

APIWhy
@import("std")Every Zig file uses it often
std.debug.printBasic output
std.testing.expectBasic tests
std.mem.eqlCompare strings and slices
std.heap.page_allocatorSimple allocation
std.ArrayListGrowable arrays
std.StringHashMapString-key maps
std.fs.cwd()File operations
std.fmt.parseIntParse numbers
defer with close or freeCleanup

Do not try to memorize the whole standard library. Learn it by solving small problems.