# Appendix D. Standard Library Map

### Appendix D. Standard Library Map

The Zig standard library is imported as:

```zig
const std = @import("std");
```

The library is organized as a tree of namespaces. Most programs use only a small part of it.

This appendix lists the major areas and the names most commonly used in ordinary programs.

### D.1 Debug and Printing

```zig
std.debug
```

Printing:

```zig
std.debug.print("{d}\n", .{123});
```

Assertions:

```zig
std.debug.assert(x > 0);
```

Panic:

```zig
std.debug.panic("bad state", .{});
```

Useful names:

```zig
std.debug.print
std.debug.assert
std.debug.panic
```

### D.2 Memory

```zig
std.mem
```

Byte operations:

```zig
std.mem.copyForwards
std.mem.copyBackwards
std.mem.eql
std.mem.indexOf
std.mem.startsWith
std.mem.endsWith
```

Example:

```zig
if (std.mem.eql(u8, a, b)) {
    equal();
}
```

Splitting:

```zig
var it = std.mem.splitScalar(u8, text, ',');
```

### D.3 Heap Allocators

```zig
std.heap
```

Page allocator:

```zig
const allocator = std.heap.page_allocator;
```

General-purpose allocator:

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

const allocator = gpa.allocator();
```

Arena allocator:

```zig
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
```

Fixed buffer allocator:

```zig
var buf: [4096]u8 = undefined;

var fba = std.heap.FixedBufferAllocator.init(&buf);
const allocator = fba.allocator();
```

### D.4 Array Lists

```zig
std.ArrayList
```

Example:

```zig
var list = std.ArrayList(u8).init(allocator);
defer list.deinit();

try list.append('A');
```

Useful fields and functions:

```zig
list.items
list.append
list.clearRetainingCapacity
list.deinit
```

### D.5 Hash Maps

```zig
std.HashMap
std.AutoHashMap
std.StringHashMap
```

Example:

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

try map.put("one", 1);
```

Lookup:

```zig
if (map.get("one")) |value| {
    use(value);
}
```

### D.6 Formatting

```zig
std.fmt
```

Formatting into memory:

```zig
const text = try std.fmt.allocPrint(
    allocator,
    "{d}",
    .{123},
);
```

Parsing:

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

Useful names:

```zig
std.fmt.allocPrint
std.fmt.bufPrint
std.fmt.parseInt
std.fmt.parseFloat
```

### D.7 Filesystem

```zig
std.fs
```

Current directory:

```zig
const cwd = std.fs.cwd();
```

Open file:

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

Create file:

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

Read file:

```zig
const data = try file.readToEndAlloc(
    allocator,
    1024 * 1024,
);
```

Useful names:

```zig
std.fs.cwd
std.fs.File
std.fs.Dir
```

### D.8 Input and Output

```zig
std.io
```

Standard output:

```zig
const stdout = std.io.getStdOut().writer();
try stdout.print("hello\n", .{});
```

Buffered writer:

```zig
var buf = std.io.bufferedWriter(stdout);
const writer = buf.writer();
```

Buffered reader:

```zig
var buf = std.io.bufferedReader(reader);
const r = buf.reader();
```

### D.9 Process and Environment

```zig
std.process
std.posix
```

Arguments:

```zig
var args = std.process.args();
```

Environment variable:

```zig
const home = std.process.getEnvVarOwned(
    allocator,
    "HOME",
);
```

Exit:

```zig
std.process.exit(1);
```

### D.10 Random Numbers

```zig
std.rand
```

Example:

```zig
var prng = std.rand.DefaultPrng.init(0);
const random = prng.random();

const x = random.int(u32);
```

### D.11 Time

```zig
std.time
```

Sleep:

```zig
std.time.sleep(1_000_000_000);
```

Timestamp:

```zig
const now = std.time.nanoTimestamp();
```

### D.12 JSON

```zig
std.json
```

Parse:

```zig
const parsed = try std.json.parseFromSlice(
    T,
    allocator,
    text,
    .{},
);
defer parsed.deinit();
```

Stringify:

```zig
try std.json.stringify(value, .{}, writer);
```

### D.13 Compression

```zig
std.compress
```

Common formats include gzip and zlib.

### D.14 Cryptography

```zig
std.crypto
```

Hashing:

```zig
std.crypto.hash.sha2
```

Random bytes:

```zig
std.crypto.random
```

Example:

```zig
var bytes: [32]u8 = undefined;
std.crypto.random.bytes(&bytes);
```

### D.15 Networking

```zig
std.net
```

TCP connection:

```zig
const stream = try std.net.tcpConnectToHost(
    allocator,
    "example.com",
    80,
);
defer stream.close();
```

### D.16 Testing

```zig
std.testing
```

Equality:

```zig
try std.testing.expect(x == y);
```

Deep equality:

```zig
try std.testing.expectEqual(a, b);
```

String equality:

```zig
try std.testing.expectEqualStrings(a, b);
```

Allocator for tests:

```zig
const allocator = std.testing.allocator;
```

### D.17 Math

```zig
std.math
```

Examples:

```zig
std.math.max
std.math.min
std.math.clamp
std.math.sqrt
std.math.sin
std.math.cos
```

### D.18 Unicode

```zig
std.unicode
```

UTF-8 validation:

```zig
const ok = std.unicode.utf8ValidateSlice(text);
```

### D.19 Build System

```zig
std.Build
```

Example:

```zig
pub fn build(b: *std.Build) void {
    const exe = b.addExecutable(.{
        .name = "app",
        .root_source_file = b.path("main.zig"),
    });

    b.installArtifact(exe);
}
```

### D.20 Common Program Skeleton

Most small Zig programs use a pattern like this:

```zig
const std = @import("std");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();

    const allocator = gpa.allocator();

    const stdout = std.io.getStdOut().writer();

    try stdout.print("hello\n", .{});

    _ = allocator;
}
```

The standard library stays close to the language itself: explicit allocation, explicit errors, explicit cleanup, and direct access to operating system facilities.

