← Sunday, September 27, 2026Languages & toolsDeep dive

Rust’s types make “parse, don’t validate” invariants explicit

eli.thegreenplace.net 8 min read Editor score 6/10

The piece applies the pattern to Rust, using `Option` and configuration-directory examples to show how APIs can represent whether a value meets an invariant. It’s useful guidance for designing safer interfaces and making invalid states harder to pass around.

What
“Parse, don't validate” applied to Rust encodes invariants in types rather than relying on checks and comments.
How
A `Vec` can remain empty after runtime validation, while `NonEmpty<T>` guarantees a head and makes `first()` return `&T`.
Why it matters
Encoding invariants in API types avoids repeated empty-case handling and makes assumptions visible to callers and the type system.

Discussion

Discussion is not switched on yet. Use Note above to keep your own thoughts on this story.

Headline, summary, brief and diagram are written by a model from the source's own text; the link, byline and numbers come from the source. Read the original before relying on a claim.

Your note

Private to you. Stored in this browser, and synced to your account when you are signed in.