[go: up one dir, main page]

Crate bon

source ·
Expand description
bon logo

crates.io Rust Documentation

bon is a Rust crate for generating compile-time-checked builders for functions and structs.

Visit the guide for a complete overview of the crate.

§Quick example

bon can turn a function with positional parameters into a function with “named” parameters via a builder. It’s as easy as placing the #[builder] macro on top of it.

use bon::builder;

#[builder]
fn greet(name: &str, age: u32) -> String {
    format!("Hello {name} with age {age}!")
}

let greeting = greet()
    .name("Bon")
    .age(24)
    .call();

assert_eq!(greeting, "Hello Bon with age 24!");

See the guide for the rest.

§License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Macros§

Attribute Macros§

  • Companion macro for builder. You should place it on top of the impl block where you want to define methods with the builder macro.
  • Can be placed on top of a free function or an associated method or a struct declaration. Generates a builder for the item beneath it.