Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

unwrap() on External Input

Smell. unwrap(), expect(), or panicking indexing on paths where the input isn’t controlled: parsing external data, network reads, file I/O, or lock acquisition in long-running services.

Why generation produces it. Training examples unwrap freely, because documentation and snippets legitimately do. Unwrap also makes the happy path compile immediately, and the tests the agent writes for itself rarely feed it bad input.

Principle. A panic on input is the dishonest-signature case from the fallibility page. Panics are for broken invariants, that’s, for states the design declared impossible. Malformed frames, short reads, and missing keys aren’t broken invariants. They are ordinary values from the outside world, and the type for ordinary failure is Result.

Prompt. “This function handles external input and must not panic. Return Result with a typed error for each failure mode. expect() is acceptable only for invariants that no input can violate, and each use must state in its message why it’s unreachable.”

The expect-with-justification rule works like the clone rule: every remaining use carries its own argument, where the next reviewer can check it.