Skip to content

into

Applies to: struct fields function arguments method arguments

TIP

This attribute is also configurable via the top-level #[builder(on(...))]

Changes the signature of the setters to accept impl Into<T>, where T is the underlying type of the member.

For optional members, the maybe_{member}() setter method will accept an Option<impl Into<T>> type instead of just Option<T>.

For members that use #[builder(default = expression)], the expression will be converted with Into::into.

This parameter is often used with the String type, which allows you to pass &str into the setter without calling .to_owned() or .to_string() on it.

See the Into Conversions In-Depth page that shows the common patterns and antipatterns of impl Into<T>.

Examples

rust
use bon::Builder;

#[derive(Builder)]
struct Example {
    #[builder(into)] 
    name: String,

    #[builder(into)] 
    description: Option<String>,

    // The value passed to `default = ...` is converted with `into` as well
    #[builder(into, default = "anon")]                                      
    group: String
}

Example::builder()
    // We can pass `&str` because the setters accept `impl Into<String>`
    .name("Bon")                                                              
    .description("Awesome crate 🐱. Consider giving it a star on Github ⭐") 
    // We can pass `Option<&str>` to `maybe_` methods because they accept
    // `Option<impl Into<String>>`
    .maybe_group(Some("Favourites"))                                          
    .build();
rust
use bon::builder;

#[builder]
fn example(
    #[builder(into)] 
    name: String,

    #[builder(into)] 
    description: Option<String>,

    // The value passed to `default = ...` is converted with `into` as well
    #[builder(into, default = "anon")]                                      
    group: String
) {}

example()
    // We can pass `&str` because the setters accept `impl Into<String>`
    .name("Bon")                                                              
    .description("Awesome crate 🐱. Consider giving it a star on Github ⭐") 
    // We can pass `Option<&str>` to `maybe_` methods because they accept
    // `Option<impl Into<String>>`
    .maybe_group(Some("Favourites"))                                          
    .call();
rust
use bon::bon;

struct Example;

#[bon]
impl Example {
    #[builder]
    fn example(
        #[builder(into)] 
        name: String,

        #[builder(into)] 
        description: Option<String>,

        // The value passed to `default = ...` is converted with `into` as well
        #[builder(into, default = "anon")]                                      
        group: String
    ) {}
}

Example::example()
    // We can pass `&str` because the setters accept `impl Into<String>`
    .name("Bon")                                                              
    .description("Awesome crate 🐱. Consider giving it a star on Github ⭐") 
    // We can pass `Option<&str>` to `maybe_` methods because they accept
    // `Option<impl Into<String>>`
    .maybe_group(Some("Favourites"))                                          
    .call();