liveBook · Manning

Chapter 4: Structuring programs, from REPL to webpage

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

The chapter introduces a word ladder game that will be built first as a REPL and then as a web application.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces a word ladder game that will be built first as a REPL and then as a web application.

Key terms: word ladder, randomized starting word

4.1 Climbing the ladder

Introduces the dictionary’s two roles in the word ladder game: checking words and supplying starting words.

Key terms: dictionary, word validity, start the ladder

4.1.1 Thinking in comptime

This section explains how comptime can move checks for unchanging data from program startup to compilation, then outlines how to prepare a word ladder dictionary.

Key terms: comptime, compilation, unchanging data, word ladder dictionary, four-character words, array of four-character strings

4.1.2 Comptime caveats

This section explains why Zig restricts comptime code and how to respond when compilation reaches its evaluation limit.

Key terms: comptime, build hermeticity, build system, backwards branches, @setEvalBranchQuota, comptime expressions

4.1.3 Executing our plan

This section builds a compile-time wordlist parser and explains the comptime features used to run and check it.

Key terms: comptime type parameter, generic function, duck typed, wordlist, std.mem.tokenizeScalar, countWordlist, comptime, @compileError, comptime memoization, side effects

4.1.4 Talk to me

This section explains how the word-ladder game can read player input, display feedback, and let the player end the interaction.

Key terms: word ladder, end, stdin, stdout, delimiter, takeDelimiter, slice, reader’s internal buffer, REPL, console applications

4.1.5 Core logic

The section uses word-ladder input validation to show how distinct errors can support useful feedback while keeping validation separate from the console interface.

Key terms: error set, error union, [4]u8, word validation, separation of concerns, console environment

4.1.6 Check it

The section finishes the terminal word-ladder game by choosing a random starting word, tracking ladder words, and turning validation errors into useful messages.

Key terms: std.Random, std.Random.DefaultPrng, seed, entropy source, std.ArrayList, allocator, items, managed, unmanaged, switch expression

4.2 Wilda’s want for words

This section explains why a browser is a practical way to make the Zig word ladder game accessible to a non-technical player across devices.

Key terms: browser, web applications, Zig’s standard library, command line, native applications

4.2.1 Connecting the web

This section introduces the protocol layers beneath the web and explains why Zig’s minimal HTTP library leaves programmers close to the flow of data.

Key terms: protocol, message format, payload, metadata, network layers, IP, TCP, HTTP, hypertext semantics, batteries-included web framework

4.2.2 A poor conversationalist

The section explains how a TCP echo server listens for connections, uses an address and port, and reads and responds over a network stream.

Key terms: TCP server, std.Io, listen, accept, address, port, port 80, network interfaces, 0.0.0.0, loopback device

4.2.3 How do I HTTP?

The section explains how HTTP builds on a TCP server and how requests and responses express what clients want and what happened.

Key terms: HTTP, TCP connection, std.http, std.net TCP server, request, response, resources, GET, POST, status code

4.2.4 The HTTP cherry on top

The section shows how the echo server handles HTTP requests and errors, then checks those behaviors with curl.

Key terms: POST, GET, 200, 400 (Bad Request), request buffer, errdefer, 500 (Internal Server Error), catch and log, separating different concerns

4.2.5 Modularity makes it easy

The section maps the modular word ladder game onto HTTP endpoints and explains how to make the resulting website available locally and on a shared network.

Key terms: interface, endpoint, GET, POST, route, word ladder validation, modular, HTTP response, 200, 400

4.3 Next steps

This section draws out the chapter’s takeaway that upfront work in Zig can make a project easier to maintain, extend, and repurpose, then suggests ways to experiment with the word game.

Key terms: Zig’s ethos, maintain, extend, repurpose

4.4 Summary

Concepts explained in this chapter

← Chapter 3: I/O with utility Chapter 5: A fabulous FFI cookbook →

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.