#[builder]
Expand description
Generates a builder for the function or method it’s placed on.
§Quick examples
You can turn a function with positional parameters into a function with
named parameters just by placing the #[builder]
attribute on top of it.
ⓘ
use bon::builder;
#[builder]
fn greet(name: &str, level: Option<u32>) -> String {
let level = level.unwrap_or(0);
format!("Hello {name}! Your level is {level}")
}
let greeting = greet()
.name("Bon")
.level(24) // <- setting `level` is optional, we could omit it
.call();
assert_eq!(greeting, "Hello Bon! Your level is 24");
You can also use the #[builder]
attribute with associated methods:
ⓘ
use bon::bon;
struct User {
id: u32,
name: String,
}
#[bon] // <- this attribute is required on impl blocks that contain `#[builder]`
impl User {
#[builder]
fn new(id: u32, name: String) -> Self {
Self { id, name }
}
#[builder]
fn greet(&self, target: &str, level: Option<&str>) -> String {
let level = level.unwrap_or("INFO");
let name = &self.name;
format!("[{level}] {name} says hello to {target}")
}
}
// The method named `new` generates `builder()/build()` methods
let user = User::builder()
.id(1)
.name("Bon".to_owned())
.build();
// All other methods generate `method_name()/call()` methods
let greeting = user
.greet()
.target("the world")
// `level` is optional, we can omit it here
.call();
assert_eq!(user.id, 1);
assert_eq!(user.name, "Bon");
assert_eq!(greeting, "[INFO] Bon says hello to the world");
The builder never panics. Any mistakes such as missing required fields or setting the same field twice will be reported as compile-time errors.
See the full documentation for more details: