【Rust设计模式之建造者模式】
#[derive(Debug)]
struct children {
name: String,
age: u32,
}
impl children {
pub fn find_generator() -> Generator {
Generator::default()
}
}
#[derive(Default)]
struct Generator {
name: String,
age: u32,
//... and so on...
}
impl Generator {
pub fn new() -> Generator {
Generator {
name: String::from("generator"),
age: 11,
//... and so on...
}
}
pub fn buildchildren(self) -> children {
children {
name: self.name,
age: self.age,
//... and so on...
}
}
}
fn main() {
let child_from_generator = Generator::new().buildchildren();
println!("{:?}", child_from_generator);
}