derive_builder

A Rust derive macro that generates the builder pattern for arbitrary structs

Library
Cargo
v0.20.2
1,539stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
48/100Fair
Development Activity4
Maintenance32
Community56
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
84/100Excellent
Architecture85
Code Quality86
Innovation82
Learning Curve82

derive_builder is a Rust procedural macro that automatically implements the builder pattern for your structs. Adding a single #[derive(Builder)] attribute to a struct Foo generates a FooBuilder type with fluent setter methods for every field and a build method that assembles the final value, eliminating the boilerplate you would otherwise write by hand.

The macro is highly configurable through builder attributes: you can convert setter inputs with Into, mark fields optional or defaulted, add validation in a custom build function, rename methods, and control mutability. It is one of the most widely used code-generation crates in the Rust ecosystem.

What You Get

  • A generated <Name>Builder type with fluent, chainable setter methods
  • A build() method that validates required fields and returns your struct or an error
  • Attribute options for Into conversions, defaults, optional fields, and renaming
  • Support for custom validation via a user-provided build function
  • no_std and no_alloc compatibility for constrained environments

Common Use Cases

  • Constructing structs that have many optional or defaulted configuration fields
  • Providing a clean public constructor API for library configuration types
  • Building complex request or options objects with readable, chained calls
  • Avoiding hand-written builder boilerplate across large codebases

Under The Hood

Architecture - The project is a Cargo workspace split into derive_builder (the public crate re-exporting the macro and runtime pieces), derive_builder_macro (the thin proc-macro entry point), and derive_builder_core (the code-generation logic that parses #[builder(...)] attributes and emits the builder struct, setters, and build fn). Dedicated no_std and no_alloc test crates verify constrained targets.

Tech Stack - Rust (MSRV 1.56) built on the syn/quote/proc-macro2 stack for parsing and token generation. Managed with Cargo; a dev/ directory holds development tooling.

Code Quality - A mature, heavily depended-upon crate (over 165M downloads) with a maintained CHANGELOG, extensive compile-and-run tests including UI tests for macro diagnostics, and separate test crates for no_std/no_alloc guarantees. Recent development activity is modest but the API is stable.

API Design - The core experience is a single #[derive(Builder)], with a rich but discoverable attribute vocabulary (setter(into), default, build_fn, field-level overrides) that scales from trivial cases to complex validation without leaving the derive.

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