typed-builder

Rust derive macro that generates compile-time type-checked builders with required-field enforcement.

Library
Cargo
v0.23.2
1,201stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
64/100Good
Development Activity56
Maintenance48
Community52
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
86/100Excellent
Architecture90
Code Quality88
Innovation86
Learning Curve78

typed-builder is a Rust procedural derive macro that generates a builder for your struct whose correctness is verified entirely at compile time. Every mandatory field must be set exactly once before build() can be called, and setting a field twice or forgetting one becomes a compilation error rather than a runtime panic or Result you must unwrap.

It encodes the builder’s state in generic type parameters, so the compiler only monomorphizes the code paths you actually use, keeping build times and generated code lean. Fields can be marked optional with defaults, have setters accept Into values, or strip Option wrappers, giving ergonomic named-and-optional-argument construction with zero runtime overhead.

What You Get

  • A #[derive(TypedBuilder)] macro that generates a full builder for any struct
  • Compile-time verification that every required field is set exactly once before build()
  • Field attributes for defaults, Into-accepting setters, and Option stripping
  • Fields settable in any order with named-argument ergonomics
  • Zero runtime overhead — no Result to unwrap, no runtime state checks

Common Use Cases

  • Building structs with many optional fields without a telescoping constructor
  • Providing a safe, discoverable construction API for a public library type
  • Replacing runtime-checked builders that force callers to unwrap a Result

Under The Hood

Architecture - The project is a Cargo workspace: the typed-builder crate re-exports the derive macro implemented in the sibling typed-builder-macro proc-macro crate. The macro reads the struct’s fields and generates a builder struct plus a chain of setter methods where each field’s set/unset status is carried in a generic type parameter, so build() is only implemented for the fully-populated type state.

Tech Stack - Rust (edition 2024). typed-builder depends only on typed-builder-macro, which uses the standard proc-macro toolchain (syn/quote/proc-macro2) to parse and emit code. No runtime dependencies are pulled into consumer crates beyond the generated code.

Code Quality - Well-established (91M+ downloads) with an examples directory, a maintained CHANGELOG, rustfmt config, and CI. Because guarantees are encoded in generics, the generated code carries no runtime checks; the trade-off, documented candidly in the README, is verbose internal builder type names not meant to be passed around.

API Design - Excellent ergonomics: a single #[derive(TypedBuilder)] plus expressive #[builder(…)] field attributes (default, setter(into), setter(strip_option)) cover most needs. Errors for missing/duplicate fields surface via deprecation-style diagnostics, and generated builder() docs aid discoverability.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search