Top-Level Attributes ​
These attributes are placed on top of a struct
or fn
declaration.
Attribute | Short description |
---|---|
builder_type | Overrides name, visibility and docs for the builder struct |
crate | Overrides path to bon crate referenced in the generated code |
derive | Generates additional derives for the builder struct itself |
finish_fn | Overrides name, visibility and docs for the finishing function |
on | Applies member attributes to all members matching a type pattern |
start_fn | Overrides name, visibility and docs for the starting function |
state_mod | Overrides name, visibility and docs for the builder's typestate API module |
Member Attributes ​
These attributes are placed on a struct
field or fn
argument.
Attribute | Short description |
---|---|
default | Makes the member optional with a default value |
finish_fn | Makes the member a positional argument on the finishing function |
into | Changes the signature of the setters to accept impl Into<T> |
name | Overrides the name of the member used in the builder's API |
overwritable 🔬 | Allows calling setters for the same member repeatedly |
required | Disables Option<T> special handling, makes the member required |
setters | Overrides name, visibility and docs for setters |
skip | Skips generating setters for the member |
start_fn | Makes the member a positional argument on the starting function |
with | Overrides setters' signature and applies a custom conversion |
Examples ​
rust
use bon::Builder;
#[derive(Builder)]
#[builder(finish_fn = finish)] // <-- this is a top-level attribute
struct Example {
#[builder(default)] // <-- this is a member attribute
field: u32
}
rust
use bon::builder;
#[builder(finish_fn = finish)] // <-- this is a top-level attribute
fn example(
#[builder(default)] // <-- this is a member attribute
arg: u32
) { }
rust
use bon::bon;
struct Example;
#[bon]
impl Example {
#[builder(finish_fn = finish)] // <-- this is a top-level attribute
fn example(
#[builder(default)] // <-- this is a member attribute
arg: u32
) { }
}