Leanpub Header

Skip to main content

The bon Cookbook: Ergonomic Builders in Rust

Master the bon Crate: Ergonomic, Typestate-Driven Builders for Rust Developers

Stop writing builder boilerplate by hand. The bon crate generates compile-time-checked builders in Rust with a single annotation — and this cookbook shows you exactly how to use it, recipe by recipe.

Minimum price

$12.99

$19.99

You pay

$19.99

Author earns

$15.99
$

...Or Buy With Credits!

You can get credits with a paid monthly or annual Reader Membership, or you can buy them here.
PDF
EPUB
WEB
89
Pages
10,757Words
About

About

About the Book

The bon crate generates compile-time-checked builders for Rust structs, functions, and methods — no boilerplate, no runtime panics for missing fields, and a typestate design that makes the compiler enforce correctness for you.

The bon Cookbook is a recipe-driven guide covering 61 practical, focused recipes: from basic setup through setter customization, validation patterns, async and generic functions, testing strategies, and a side-by-side comparison with derive_builder and typed-builder.

For intermediate Rust developers who want to ship cleaner, safer APIs without writing builder boilerplate by hand

Share this book

Author

About the Author

Eugenio Perrotta Neto

Eugenio Perrotta Neto is a Senior Backend Engineer with 19 years of experience building high-performance systems. He currently works on low-latency trading infrastructure in Rust at a Brazilian market-making firm integrated with the B3 exchange. He writes about systems programming, Rust, and the lessons learned building production software at scale.

Contents

Table of Contents

Chapter 1: Getting Started with bon

  1. Recipe 1: Adding bon to Your Project
  2. Recipe 2: Your First #[builder] on a Free Function
  3. Recipe 3: Your First #[derive(Builder)] on a Struct
  4. Recipe 4: Using #[bon] and #[builder] on impl Blocks
  5. Recipe 5: Understanding Naming Conventions

Chapter 2: Required and Optional Members

  1. Recipe 6: Marking a Field as Required
  2. Recipe 7: Using Option for Optional Members
  3. Recipe 8: Using #[builder(default)] with the Default Trait
  4. Recipe 9: Using #[builder(default = expr)] with a Custom Expression
  5. Recipe 10: Referencing Other Members in default Expressions
  6. Recipe 11: Switching Between Option and #[builder(default)] Without Breaking Callers

Chapter 3: The Typestate Pattern Under the Hood

  1. Recipe 12: Understanding How bon Uses Typestate to Enforce Required Fields
  2. Recipe 13: Reading Compile Errors for Missing Required Members
  3. Recipe 14: Reading Compile Errors for Duplicate Setter Calls
  4. Recipe 15: Exposing the Typestate API for Custom Builder Methods
  5. Recipe 16: Adding Custom Setter Methods via impl on the Builder

Chapter 4: Setter Customization

  1. Recipe 17: Renaming a Setter with #[builder(name = ...)]
  2. Recipe 18: Enabling Into Conversions with #[builder(into)]
  3. Recipe 19: Applying Into to Multiple Fields with #[builder(on(...))]
  4. Recipe 20: Custom Setter Logic with #[builder(with = |...| ...)]
  5. Recipe 21: Skipping a Member with #[builder(skip)]
  6. Recipe 22: Overwritable Setters

Chapter 5: Working with Functions and Methods

  1. Recipe 23: #[builder] on Async Functions
  2. Recipe 24: #[builder] on Fallible Functions Returning Result
  3. Recipe 25: #[builder] on Generic Functions
  4. Recipe 26: #[builder] on Functions with impl Trait Parameters
  5. Recipe 27: #[builder] on Methods with &self and &mut self
  6. Recipe 28: #[builder] on Associated Functions Not Named new
  7. Recipe 29: Handling Destructuring Patterns

Chapter 6: Visibility and Documentation

  1. Recipe 30: Controlling Visibility of the Generated Builder Struct
  2. Recipe 31: Controlling Visibility of Individual Setters
  3. Recipe 32: Adding Doc Comments to the Builder and Its Methods
  4. Recipe 33: Documenting Optional vs Required Fields in Generated Docs
  5. Recipe 34: Hiding Builder Internals from Public API with Visibility Tricks

Chapter 7: Validation and Finishing Functions

  1. Recipe 35: Validating Inputs Inside the Constructor Function Body
  2. Recipe 36: Renaming build() with #[builder(finish_fn = ...)]
  3. Recipe 37: Making the Finish Function Fallible
  4. Recipe 38: Customizing the Start Function Name with start_fn
  5. Recipe 39: Building a Validated Domain Type with a Private Struct and Builder Function

Chapter 8: Getters and Derives on the Builder

  1. Recipe 40: Generating Getters for Builder Fields with #[builder(getter)]
  2. Recipe 41: Configuring Getter Visibility and Naming
  3. Recipe 42: Deriving Traits on the Builder with #[builder(derive(...))]
  4. Recipe 43: Generating impl From for T with #[builder(derive(Into))]

Chapter 9: Patterns for Real-World APIs

  1. Recipe 44: Builder for a Configuration Struct
  2. Recipe 45: Builder for a Query or Request Object
  3. Recipe 46: Conditional Building Based on Optional Fields
  4. Recipe 47: Composing Builders
  5. Recipe 48: Builder with serde::Deserialize on the Same Struct
  6. Recipe 49: Builder with Clone and Debug Derives

Chapter 10: Shared Configuration and Reuse

  1. Recipe 50: Applying the Same #[builder(...)] Config to Many Structs with macro_rules_attribute
  2. Recipe 51: Wrapping bon Macros in Your Own Proc Macro
  3. Recipe 52: Controlling the Builder Type Name

Chapter 11: Testing and Ergonomics

  1. Recipe 53: Testing Builders in Unit Tests
  2. Recipe 54: Using experimental-overwritable to Reduce Boilerplate in Test Fixtures
  3. Recipe 55: Snapshot Testing Builder Outputs with insta
  4. Recipe 56: IDE Autocomplete and Rust Analyzer Integration

Chapter 12: bon vs. The Alternatives

  1. Recipe 57: bon vs. derive_builder
  2. Recipe 58: bon vs. typed-builder
  3. Recipe 59: Migrating from derive_builder to bon
  4. Recipe 60: Migrating from typed-builder to bon
  5. Recipe 61: When Not to Use a Builder

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub