liveBook · Manning

Chapter 3: I/O with utility

What you will learn in chapter 3 of Systems Programming with Zig

Introduces cat as a simple, useful program and argues that dependable everyday software is a worthwhile use for Zig.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces cat as a simple, useful program and argues that dependable everyday software is a worthwhile use for Zig.

Key terms: cat, performance, reliability, concatenating files, console

3.1 Standard Zig

This section introduces the role and optionality of Zig’s standard library and shows where to find its documentation.

Key terms: standard library, std, standard library documentation, language documentation, zig std

3.1.1 First and Foremost

Explains how Zig startup code supplies an optional init argument to main and what its two supported types contain.

Key terms: start.zig, init, std.process.Init.Minimal, std.process.Init, std.Io

3.1.2 Allocating with Allocators

This section explains why Zig uses explicit allocators, how allocated memory is managed, and what several common allocators offer.

Key terms: stack memory, heap memory, allocator, system call, free, allocator paradigm, memory management, language runtime, SafeAllocator, ArenaAllocator

3.1.3 Not just I/O

This section explains why Zig’s std.Io groups several system capabilities together, distinguishes async from concurrency, and identifies the Io instance the book will usually use.

Key terms: std.Io, Io instance, file system, networking, randomness, time, asynchronous, async, concurrent, runtime

3.2 Meow, says cat

The section defines the cat program’s basic behavior and introduces an iterative approach to implementing it.

Key terms: cat, command line arguments, stdout

3.2.1 Adopting our first cat

The section introduces reading a file for a cat program using Zig’s file and directory handles and an allocator.

Key terms: std.Io.File, std.Io.Dir, handle, relative paths, std.Io.Dir.cwd, readFileAlloc, init.arena.allocator(), current working directory

3.2.2 Errors, catch and try

This section explains how Zig represents errors with error sets and error unions, then how callers handle or propagate those errors explicitly.

Key terms: error sets, anyerror, coerce, error union, !, payload, if/else, capture groups, catch, try

3.2.3 The interface pattern

This section explains how Zig writers serve as interfaces to different output implementations and why that interface machinery is explicit.

Key terms: std.Io.Writer, generic interfaces, underlying implementation, allocators, anyopaque, cast, interface boilerplate, object oriented features

3.3 Giving our cat more toys

This section introduces the cat prototype’s missing features and previews loops as a prerequisite for handling multiple files.

3.3.1 For loops

This section shows how Zig for loops iterate over arrays, slices, ranges, and multiple inputs, and how they can produce values or skip iterations.

Key terms: for loops, indexable types, capture group, multiple iterables, captures, produce, counts, range, index, usize

3.3.2 Cat but it can concatenate

This subsection extends the cat program to concatenate files named on the command line and shows how to pass those names through zig run.

Key terms: std.process.Init, command line arguments, for loop, executable, zig run, two dashes

3.3.3 While loops

Introduces Zig while loops through a Collatz example and notes their built-in support for an end-of-iteration operation.

Key terms: while, Collatz sequence, iterations, while loops, end of every iteration, built-in syntax

3.4 Questioning our cat

Introduces how Zig’s error features can help make the cat program more reliable by revealing possible failures and where they can occur.

Key terms: type-checked error sets, error-as-value semantics, syntax markers

3.4.1 All of the cat errors

Revisits the cat program to identify possible failures and decide which errors deserve user-facing handling rather than propagation.

Key terms: try, return, error sets, main, stdout, CLI arguments, readFileAlloc, arena allocator, out of memory

3.4.2 Defer to better resource management

This section shows how to free each file’s contents reliably in the cat program by using a general purpose allocator and defer.

Key terms: arena allocator, general purpose allocator, gpa, free, defer, scope, try, errdefer

3.4.3 Improvement can be a waste of time

The section explains how to judge whether improving a program is worth the human effort, using the cat program to distinguish a practical need from a learning exercise.

Key terms: performance, reliability, human tradeoffs, maintenance, operating cost, reinventing wheels, proof of concept, minimal working program, cat, nuclear grade

3.4.4 Making the wheel nuclear grade

This section reduces a cat-like program to its essential file operations and error choices, then identifies an unnecessary whole-file allocation.

Key terms: operating system, stdout, stderr, readFileAlloc, buffer, out of memory

3.4.5 Writer, meet reader

The section introduces std.Io.Reader and shows how pairing a reader with a writer lets the cat program stream file data.

Key terms: std.Io.Reader, writer, streaming, std.process.exit, non-zero code

3.5 Buffering and building with Buff and Bill

Introduces conflicting buffer-size needs for the cat program and proposes compile-time configuration through a build script.

Key terms: comptime configuration, build script, buffer size

3.5.1 Why do we need build scripting?

Explains why a Zig project may move from `zig run` to a build script and introduces build steps and build graphs.

Key terms: zig run, build script, compilation tasks, Zig modules, build steps, build graph, build scripts, Zig programs, modularize, package

3.5.2 Our build script

This section sets up a Zig build script for the cat program and explains how its executable is built, configured, and located.

Key terms: src, main.zig, build.zig, build function, std.Build, build graph, createModule, addExecutable, install step, target

3.5.3 To buffer, or not to buffer

This section uses build options and comptime values to produce cat programs with different buffering behavior, while introducing Zig optionals.

Key terms: optionals, ?T, null, if, capture group, .?, orelse, zig build, options, readByte

3.5.4 Say goodbye to our cats

The section contrasts the satisfaction of making elegant code with the harder work of making software useful to real users.

Key terms: useful software, real users, ever-changing requirements

3.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 2: A practical guide Chapter 4: Structuring programs, from REPL to webpage →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.