Skip to content

Chapter 17: Predict-Then-Read

Twenty-four short programs, six each in Go, Rust, Python, and TypeScript, and for every one of them the exact output and a line-by-line explanation of why the language produces it. Each snippet is aimed at a belief that is common, load-bearing, and wrong: about defer, integer overflow, what a closure captures, or what this is bound to.

Concepts from other sections (4)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

This is one of the two parts of the practice section. The build part explains how whole programs are put together. This part explains the rules underneath them, one surprising line at a time.

Learning objectives

  • Predict the exact output of short programs in Go, Rust, Python, and TypeScript, and name the rule behind each line.
  • Recognise the common wrong prediction for each snippet and the misconception that produces it.
  • Connect one underlying model (value versus reference, evaluation time, bytes versus characters) across languages.
  • Reproduce any output yourself, and compare what each runtime costs to start.

Why predicting output builds a correct mental model

Section titled “Why predicting output builds a correct mental model”

Reading a language spec produces recognition (“yes, that looks right”), which is not the same as understanding. Committing to an exact expected output before you look forces your model of the language into a form that can be falsified. When it is wrong, you get a precise, memorable correction instead of a vague feeling that you should reread the chapter.

The explanations are built for that moment. Each snippet opens with the rule: the language semantics that decide its output, stated from first principles, in terms of what the runtime actually stores and does. Then, for each output line, the explanation applies that rule to the exact code in the snippet and, where it matters, gives the common wrong prediction: what most people write down, and the misconception that produces it.

That last part is the one to dwell on. A wrong prediction is almost never a typo; it is a wrong model, and the same wrong model will produce wrong code.

Start by reading the snippet source; each entry links it. Then predict the full output, line by line, and write it down: exact text, not “roughly this”. Only then open the answer (the folded “Output and explanation” block) and compare. For every line you missed, read the explanation until you can state the rule in one sentence without looking. If you can also explain why the common wrong prediction is wrong, you have the model.

Go slowly. The value is in the minute you spend deciding what you think happens, and a prediction you change after seeing the answer tells you nothing.

Each language has one page of worked answers, and the snippets live beside it:

You can read the languages in any order; the pairings described after the tables below are a good reason to read across them rather than one at a time.

The tables below list every snippet by its file name, with a hint at the trap it sets, and link straight to its worked answer.

#GoRust
01loop-var-capture (Go 1.22 changed the answer)shadowing-and-blocks
02nil-interface (typed nil is not nil)drop-order (including let _ =)
03slice-aliasing (when append shares, when it copies)iterator-laziness
04defer-order (LIFO, arg evaluation, named returns)integer-overflow (wrap, saturate, as)
05strings-and-runes (bytes vs runes vs characters)string-vs-str (byte-indexed slicing)
06channels-and-close (closed channels never block)move-and-rc (watching the refcount)
#PythonTypeScript
01mutable-default-arg (evaluated once, at def)number-precision (everything is a double)
02late-binding-closuresarray-sort-and-holes (lexicographic by default)
03identity-vs-equality (is asks the allocator)equality-coercion
04aliasing-and-copies ([[0]*3]*3)this-binding
05generators-and-exhaustion (what zip eats)types-are-erased
06class-attrs-and-mro (reads walk, writes do not)async-ordering (microtasks before timers)

The cross-language pairings are the real payload. Aliasing shows up as Go slice capacity, Python list multiplication, and Rust move semantics; the same missing model of value versus reference produces all three. Overflow is a Rust API choice, a TypeScript silent precision loss, and in Go a compile error you never see. Closure capture is Go 1.22’s per-iteration variable, Python’s late binding, and TypeScript’s this. Strings are bytes in Go and Rust and both languages make len("héllo") equal 6. Reading the pairs within an hour of each other is what makes the connection stick.

The outputs on these pages were produced by running the snippets, and every snippet is deterministic: no map iteration order, goroutine interleaving, timestamps, or addresses reach the output. You can reproduce any of them. Each language keeps its snippets in the layout its toolchain expects, so no wrapper is needed, as the following table shows.

LanguageSnippet pathRun it (from the language directory)
Gogo/predict/NN-name/main.gogo run ./predict/NN-name
Rustrust/predict/src/bin/NN-name.rscargo run --quiet --bin NN-name (inside rust/predict/)
Pythonpython/predict/NN-name.pypython3 predict/NN-name.py
TypeScriptts/predict/NN-name.tsnode predict/NN-name.ts (Node 22.18+ strips types natively; older 22.x needs --experimental-strip-types)

Node runs the TypeScript files by stripping the types, not by checking them. Some snippets are deliberately unsound, because being unsound at runtime is the lesson, so do not expect tsc --noEmit to accept all of them.

What a program prints is one half of a mental model; what it costs is the other half, and usually the half made of folklore. These snippets all do nothing (under twenty printed lines, no allocation worth the name), so timing them measures the price of admission for each runtime. Measured as the minimum wall time of several runs and the peak resident memory, with Go and Rust built ahead of time so no compile step is counted:

RuntimeWall timePeak RSSWhat you are paying for
Rust~2ms~1.5MBA static binary with no runtime to start
Go~3ms~4MBThe scheduler and GC coming up before main
Python~19ms~15MBInterpreter startup plus stdlib import
Node~50ms~70MBV8 reserving its heap

Minimum, not mean, because every source of noise makes a run slower and never faster. Peak, not average, because peak memory is what gets a process killed. A 45x memory spread between the fastest and slowest way to print twelve lines is worth knowing before you pick a language for something that starts a process per request.