liveBook · Manning

Chapter 2: A practical guide

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

The introduction frames Zig as a pragmatic language and explains why the book teaches basics before moving to practical projects.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction frames Zig as a pragmatic language and explains why the book teaches basics before moving to practical projects.

Key terms: real-world pragmatism, basics, projects with real-world application

2.1 The anatomy of a Zig program

This section introduces the imports, modules, functions, and execution-time distinctions in a simple Zig program.

Key terms: std, @import, namespace, std.debug.print, builtin, module, root source file, @ prefix, function body, block

2.1.1 Variables

This section explains how Zig declares variables, chooses between const and var, and determines and enforces their types.

Key terms: const, var, statically typed, type annotation, inferred, i32, bool, strongly typed, type coercion

2.1.2 Basic types

Introduces Zig’s basic types in the context of type annotations and points readers to a table that is not included in the supplied text.

2.1.3 Functions

Uses a recursive Fibonacci example to explain Zig function signatures and returns, overflow behavior, and conditional expressions.

Key terms: usize, i32, const, void, return, integer overflow, safe mode, runtime panic, +, +%

2.1.4 Back to modules

This section uses a Fibonacci example to explain how Zig source files expose declarations, how @import accesses them, and why main must be public.

Key terms: pub, source file, @import, namespace, declarations, main, start code, root

2.1.5 That’s it?

This section explains why Zig’s small set of program-building tools can still scale to serious work.

Key terms: composability, interchangeable parts, well-refined semantics

2.2 Pointing You in the Right Direction

Introduces Zig pointers as raw memory addresses whose permissiveness provides flexibility while allowing memory-access errors.

Key terms: raw machine pointers, memory addresses, uninitialized, segmentation fault, segfault, mutating data, strings, arrays, function pointers

2.2.1 Single-Item Pointers

This section explains how Zig single-item pointers represent and access one value, and how the referenced value’s mutability affects pointer use.

Key terms: single-item pointer, item, *i32, &, .*, dereference, mutable pointer, constant pointer, *const i32, coerce

2.2.2 Arrays

This section explains Zig array syntax, value-copying behavior, and why functions may take a pointer when they need to mutate an array.

Key terms: [N]T, inferred element count, zero-indexed, arrays are values, arr_copy, const, var, *[N]T, *const [N]T, single-item pointer

2.2.3 Slicing and dicing

This section explains how Zig slices represent and safely access a variable number of elements, how they compare with many-item pointers, and how to obtain them from arrays and ranges.

Key terms: slice, []T, []const T, ptr, len, safe mode, many-item pointer, [*]T, [*]const T, coerce

2.2.4 Buffers and Undefined

This section explains how Zig programs use byte buffers, how `undefined` can create an uninitialized buffer, and why reading that buffer before writing to it is dangerous.

Key terms: buffer, array, slice, []u8, undefined, uninitialized, stdout_file.writer, *[1024]u8, stdout

2.2.5 I’ve been Stringing You Along

This section explains how Zig represents strings and character literals using byte sequences, pointers, slices, and integer values rather than dedicated string or character types.

Key terms: string type, UTF-8, slices, string literal, single-item pointer, u8, []const u8, coerce, :0, nul terminator

2.2.6 A few points to review

This review emphasizes that Zig’s pointer-related types have distinct purposes when writing code that deals with memory.

Key terms: pointer-related types, expressive type system

2.3 Structs

This section introduces Zig structs as composite values, explains how their fields are initialized and accessed, and shows how structs can be used as values.

Key terms: struct, composite type, fields, dot syntax, structs, values, function parameters

2.3.1 Structs are also namespaces

This section shows that a Zig struct can hold declarations and serve as a namespace without grouping data.

Key terms: struct, namespace, fields, declarations, source files

2.3.2 Methods, sort of

Explains Zig’s method-like dot syntax for struct functions and why mutating an instance requires a pointer parameter.

Key terms: struct, dot syntax, method syntax, Rectangle.size, *Rectangle, value, copy

2.3.3 Struct initialization in the wild

This section explains common Zig struct initialization patterns and why explicit initializers can be preferable to default field values.

Key terms: Stack.empty, Stack.init(), .empty, .init(), initializer functions, initializer constants, default field values, canned_tuna

2.3.4 Simple and sweet

This section sums up Zig structs as deliberately simple and connects that simplicity to Zig’s different design priorities.

Key terms: structs, fields, declarations, inheritance, virtual method tables, syntax sugar, priorities, ergonomics

2.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 1: Zig’s big ideas Chapter 3: I/O with utility →

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.