Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug)]
- struct MyThing {
- foo: Vec<String>,
- bar: Vec<i32>
- }
- impl MyThing {
- pub fn new(foo: Vec<String>, bar: Vec<i32>) -> Self {
- MyThing {
- foo, bar
- }
- }
- }
- struct MyThingBuilder {
- foo: Vec<String>,
- bar: Vec<i32>
- }
- impl MyThingBuilder {
- pub fn new() -> Self {
- MyThingBuilder {
- foo: Vec::new(), bar: Vec::new()
- }
- }
- pub fn with_foo(mut self, foo: Vec<String>) -> Self {
- self.foo = foo;
- self
- }
- pub fn with_bar(mut self, bar: Vec<i32>) -> Self {
- self.bar = bar;
- self
- }
- pub fn build(self) -> Result<MyThing, String> {
- MyThing::new(self.foo, self.bar)
- }
- }
- fn main() {
- let blah = MyThing::new(vec!["Hello".into()], vec![42]);
- println!("{:?}", blah);
- let wooooo = MyThingBuilder::new()
- .with_foo(vec!["Hello".into()])
- .build();
- println!("{:?}", wooooo)
- }
Add Comment
Please, Sign In to add comment