Created
April 5, 2018 06:12
-
-
Save ryochack/6b341bda5c5a45024fc95de3a5a230d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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