Guest User

Untitled

a guest
Jul 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #[derive(Debug)]
  2. struct MyThing {
  3. foo: Vec<String>,
  4. bar: Vec<i32>
  5. }
  6.  
  7. impl MyThing {
  8. pub fn new(foo: Vec<String>, bar: Vec<i32>) -> Self {
  9. MyThing {
  10. foo, bar
  11. }
  12. }
  13. }
  14.  
  15. struct MyThingBuilder {
  16. foo: Vec<String>,
  17. bar: Vec<i32>
  18. }
  19.  
  20. impl MyThingBuilder {
  21. pub fn new() -> Self {
  22. MyThingBuilder {
  23. foo: Vec::new(), bar: Vec::new()
  24. }
  25. }
  26.  
  27. pub fn with_foo(mut self, foo: Vec<String>) -> Self {
  28. self.foo = foo;
  29. self
  30. }
  31.  
  32. pub fn with_bar(mut self, bar: Vec<i32>) -> Self {
  33. self.bar = bar;
  34. self
  35. }
  36.  
  37. pub fn build(self) -> Result<MyThing, String> {
  38. MyThing::new(self.foo, self.bar)
  39. }
  40. }
  41.  
  42. fn main() {
  43. let blah = MyThing::new(vec!["Hello".into()], vec![42]);
  44. println!("{:?}", blah);
  45.  
  46. let wooooo = MyThingBuilder::new()
  47. .with_foo(vec!["Hello".into()])
  48. .build();
  49. println!("{:?}", wooooo)
  50. }
Add Comment
Please, Sign In to add comment