Skip to content

name ​

Applies to: struct fields function arguments method arguments

Overrides the name of the member used in the builder's API. This is most useful when with struct syntax (#[derive(Builder)]) where you'd like to use a different name for the field internally. For functions this attribute makes less sense since it's easy to just create a variable named differently let new_name = param_name;. However, this attribute is still supported on function arguments.

Examples ​

rust
use bon::Builder;

#[derive(Builder)]
struct Player {
    #[builder(name = rank)] 
    level: u32
}

Player::builder()
    .rank(10) 
    .build();
rust
use bon::builder;

#[builder]
fn player(
    #[builder(name = rank)] 
    level: u32
) {}

player()
    .rank(10) 
    .call();
rust
use bon::bon;

struct Player {
    level: u32,
}

#[bon]
impl Player {
    #[builder]
    fn new(
        #[builder(name = rank)] 
        level: u32
    ) -> Self {
        Self { level }
    }
}

Player::builder()
    .rank(10) 
    .build();