-
Book Overview & Buying
-
Table Of Contents
Design Patterns and Best Practices in Rust
By :
Alexis King popularized the parse, don't validate principle with her influential 2019 blog post of the same name (https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/). King's articulation of the principle, which states that parsing generates types that can represent only valid data, while validation merely checks data after the fact, has its roots in functional programming and type theory and helped popularize and name the pattern.
Rust's type system makes this principle particularly natural to apply. The combination of strong typing, pattern matching, and the Result type creates an environment where parse, don't validate feels like the path of least resistance rather than additional work.
The principle suggests that instead of scattering validation logic throughout a code base, we should parse data once at system boundaries into types that can only represent valid states. This transforms validation from a repeated...