<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/rusty-thoughts-on-parse-don-t-validate--pcdgqdpoi" -->

---
title: Rusty thoughts on "Parse, don't validate" | daily.dev
description: An exploration of the 'parse, don't validate' pattern (originally coined for Haskell by Alexis King) applied to Rust, showing how the type system can eliminate...
canonical: https://daily.dev/posts/rusty-thoughts-on-parse-don-t-validate--pcdgqdpoi
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Rusty thoughts on "Parse, don't validate" | daily.dev
og:description: An exploration of the 'parse, don't validate' pattern (originally coined for Haskell by Alexis King) applied to Rust, showing how the type system can eliminate...
og:url: https://daily.dev/posts/rusty-thoughts-on-parse-don-t-validate--pcdgqdpoi
og:image: https://api.daily.dev/og/posts/PcDGqDPOi.png
og:image:alt: Rusty thoughts on "Parse, don't validate"
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Rusty thoughts on "Parse, don't validate"

**[Eli Bendersky](https://daily.dev/sources/elibendersky)** · 8 min read · 7 upvotes · 1 comments

## Summary

An exploration of the 'parse, don't validate' pattern (originally coined for Haskell by Alexis King) applied to Rust, showing how the type system can eliminate redundant runtime checks. Examples include using a NonEmpty vector type instead of Vec to guarantee non-emptiness, rust-analyzer's AbsPathBuf and camino's Utf8PathBuf for gradual type refinement of filesystem paths, Rust's built-in NonZero type for non-zero integers with a niche-optimized Option representation, and serde's JSON deserialization encoding validation directly into struct types via enums and NonZeroUsize fields.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://eli.thegreenplace.net/2026/rusty-thoughts-on-parse-dont-validate>

## Questions this post answers

### How can I make a vector type in Rust that guarantees it is never empty, so I don't have to keep unwrapping Option from first()?

Use a dedicated non-empty vector type, such as the one from the nonempty crate, which stores a head element separately from the tail Vec. Its constructor requires at least one element, so its first() method returns &T directly instead of Option<&T>, and NonEmpty::from_vec converts an existing Vec into this type, returning None only at that single validation point.

_Developers designing invariant-safe Rust APIs can find similar type-refinement patterns and discussions curated on daily.dev._

### What does Rust's NonZero type do and why is Option<NonZeroUsize> the same size as usize?

NonZero<usize> represents an unsigned integer guaranteed to never be zero, used for example as the return type of thread::available_parallelism(). Because zero is an invalid value for NonZero, Rust reuses the zero bit pattern to represent Option::None, so Option<NonZeroUsize> has the same size and alignment as NonZeroUsize and usize itself, avoiding extra storage overhead.

_Rust developers optimizing memory layout and API safety can track these type-system tricks via daily.dev._

## Community discussion

Top comments from developers on daily.dev.

**@petermrozek** · 0 upvotes

> A very nice read on the topic. "Parse, don't validate" is one of the better patterns that can make your type system do guarantees about the content. There was a second article like this, 2 years prior with a simpler example of parsing an e-mail address. If you're interested, have a nice read: [https://www.rustfinity.com/blog/parse-dont-validate](https://www.rustfinity.com/blog/parse-dont-validate)

## Similar posts on daily.dev

- [Parse, don't Validate and Type-Driven Design in Rust — ramblings of @harudagondi](https://daily.dev/posts/parse-don-t-validate-and-type-driven-design-in-rust-ramblings-of-harudagondi-je8owvusb) · Hacker News · 0 upvotes · 0 comments
- [Parse, Don't Validate — In a Language That Doesn't Want You To](https://daily.dev/posts/parse-don-t-validate-in-a-language-that-doesn-t-want-you-to-zyfa03chk) · Lobsters · 0 upvotes · 0 comments
- [When Rust Gets Ugly](https://daily.dev/posts/when-rust-gets-ugly-tyyir3taq) · corrode · 17 upvotes · 0 comments
- [Choosing Rust for All the Wrong Reasons \(Diary of a Mercenary\)](https://daily.dev/posts/choosing-rust-for-all-the-wrong-reasons-diary-of-a-mercenary--p8m8obyxf) · Medium · 3 upvotes · 0 comments

---

Tags: [#architecture](https://daily.dev/tags/architecture), [#rust](https://daily.dev/tags/rust)

[View this post on daily.dev](https://daily.dev/posts/rusty-thoughts-on-parse-don-t-validate--pcdgqdpoi)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Rusty thoughts on \"Parse, don't validate\"","url":"https://daily.dev/posts/rusty-thoughts-on-parse-don-t-validate--pcdgqdpoi","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/rusty-thoughts-on-parse-don-t-validate--pcdgqdpoi"},"datePublished":"2026-09-26T15:27:17.932Z","dateModified":"2026-09-26T15:27:40.656Z","description":"An exploration of the 'parse, don't validate' pattern (originally coined for Haskell by Alexis King) applied to Rust, showing how the type system can eliminate...","image":"https://media.daily.dev/image/upload/s--ZrL_HSsR--/f_auto/v1722860399/public/Placeholder%2006","thumbnailUrl":"https://media.daily.dev/image/upload/s--ZrL_HSsR--/f_auto/v1722860399/public/Placeholder%2006","isAccessibleForFree":true,"articleSection":"Eli Bendersky","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Eli Bendersky","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/ac2e84092d814b68aa34080a3232a3cd","url":"https://daily.dev/sources/elibendersky"},"commentCount":1,"discussionUrl":"https://daily.dev/posts/rusty-thoughts-on-parse-don-t-validate--pcdgqdpoi","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":7},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"keywords":"architecture,rust","timeRequired":"PT8M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Eli Bendersky","item":"https://daily.dev/sources/elibendersky"},{"@type":"ListItem","position":3,"name":"Rusty thoughts on \"Parse, don't validate\""}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/rusty-thoughts-on-parse-don-t-validate--pcdgqdpoi","comment":[{"@type":"Comment","text":"A very nice read on the topic. “Parse, don’t validate” is one of the better patterns that can make your type system do guarantees about the content. There was a second article like this, 2 years prior with a simpler example of parsing an e-mail address. If you’re interested, have a nice read: https://www.rustfinity.com/blog/parse-dont-validate","datePublished":"2026-09-27T10:38:41.355Z","url":"https://daily.dev/posts/PcDGqDPOi#c-NK0FMfEQv","author":{"@type":"Person","name":"Peter Mrożek","url":"https://daily.dev/petermrozek","image":"https://media.daily.dev/image/upload/s--pBfYX68K--/f_auto/v1769247960/avatars/avatar_Qz65P1nVw3Bu6C5YwaZgA?_a=BAMAMiiu0"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/rusty-thoughts-on-parse-don-t-validate--pcdgqdpoi#faq","mainEntity":[{"@type":"Question","name":"How can I make a vector type in Rust that guarantees it is never empty, so I don't have to keep unwrapping Option from first()?","acceptedAnswer":{"@type":"Answer","text":"Use a dedicated non-empty vector type, such as the one from the nonempty crate, which stores a head element separately from the tail Vec. Its constructor requires at least one element, so its first() method returns \u0026T directly instead of Option\u003c\u0026T\u003e, and NonEmpty::from_vec converts an existing Vec into this type, returning None only at that single validation point. Developers designing invariant-safe Rust APIs can find similar type-refinement patterns and discussions curated on daily.dev."}},{"@type":"Question","name":"What does Rust's NonZero type do and why is Option\u003cNonZeroUsize\u003e the same size as usize?","acceptedAnswer":{"@type":"Answer","text":"NonZero\u003cusize\u003e represents an unsigned integer guaranteed to never be zero, used for example as the return type of thread::available_parallelism(). Because zero is an invalid value for NonZero, Rust reuses the zero bit pattern to represent Option::None, so Option\u003cNonZeroUsize\u003e has the same size and alignment as NonZeroUsize and usize itself, avoiding extra storage overhead. Rust developers optimizing memory layout and API safety can track these type-system tricks via daily.dev."}}]}
```

