Skip to content

Instantly share code, notes, and snippets.

@ryochack
Created April 5, 2018 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryochack/6b341bda5c5a45024fc95de3a5a230d8 to your computer and use it in GitHub Desktop.
Save ryochack/6b341bda5c5a45024fc95de3a5a230d8 to your computer and use it in GitHub Desktop.
#[derive(Clone, Debug)]
struct AnyOption {
a: bool,
b: bool,
}
struct AnyBuilder {
option: AnyOption,
}
struct Any {
option: AnyOption,
}
impl AnyBuilder {
fn new() -> AnyBuilder {
AnyBuilder {
option: AnyOption { a: false, b: false },
}
}
fn with_a(&mut self) -> &mut Self {
self.option.a = true;
self
}
fn with_b(&mut self) -> &mut Self {
self.option.b = true;
self
}
fn build(&self) -> Any {
Any {
option: self.option.clone(),
}
}
}
fn main() {
let any = AnyBuilder::new().build();
println!("{:?}", any);
let any = AnyBuilder::new().with_a().build();
println!("{:?}", any);
let any = AnyBuilder::new().with_a().with_b().build();
println!("{:?}", any);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment