Skip to content

Instantly share code, notes, and snippets.

@rob-p
Created February 4, 2024 16:16
Show Gist options
  • Save rob-p/dbab170ce7f389c868d9c1a0d3b47919 to your computer and use it in GitHub Desktop.
Save rob-p/dbab170ce7f389c868d9c1a0d3b47919 to your computer and use it in GitHub Desktop.
A simple builder in rust (using the `derive_builder` crate).
#[macro_use]
extern crate derive_builder;
#[derive(Default, Builder, Debug)]
#[builder(setter(into))]
struct House {
floors: u32,
rooms: u32,
#[builder(default = "false")]
has_garage: bool,
}
fn main() -> Result<(), HouseBuilderError> {
let house = HouseBuilder::default()
.floors(2u32)
.rooms(3u32)
.has_garage(true)
.build()?;
println!("House : {:?}", house);
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment