Skip to content

Top-Level Attributes ​

These attributes are placed on top of a struct or fn declaration.

AttributeShort description
builder_typeOverrides name, visibility and docs for the builder struct
constMarks all generated builder functions and methods as const fn
crateOverrides path to bon crate referenced in the generated code
deriveGenerates additional derives for the builder struct itself
finish_fnOverrides name, visibility and docs for the finishing function
onApplies member attributes to all members matching a type pattern
start_fnOverrides name, visibility and docs for the starting function
state_modOverrides name, visibility and docs for the builder's typestate API module

Member Attributes ​

These attributes are placed on a struct field or fn argument.

AttributeShort description
defaultMakes the member optional with a default value
fieldDefines a private field on the builder without setters
finish_fnMakes the member a positional argument on the finishing function
getterGenerates a getter method for a member
intoChanges the signature of the setters to accept impl Into<T>
nameOverrides the name of the member used in the builder's API
overwritable 🔬Allows calling setters for the same member repeatedly
requiredDisables Option<T> special handling, makes the member required
settersOverrides name, visibility and docs for setters
skipSkips generating setters for the member
start_fnMakes the member a positional argument on the starting function
withOverrides 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
    ) { }
}