Guest User

Untitled

a guest
Oct 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. #[derive(Debug)]
  2. struct A<'a>
  3. {
  4. pub b: &'a mut B,
  5. }
  6.  
  7. impl<'a> A<'a>
  8. {
  9. fn new(b: &'a mut B) -> A<'a>
  10. {
  11. A
  12. {
  13. b: b,
  14. }
  15. }
  16.  
  17. fn foo(self) -> A<'a>
  18. {
  19. &self.b.bar();
  20.  
  21. self
  22. }
  23. }
  24.  
  25. #[derive(Debug)]
  26. struct B
  27. {
  28. b1: String,
  29. }
  30.  
  31. impl B
  32. {
  33. fn new() -> B
  34. {
  35. B
  36. {
  37. b1: String::from(""),
  38. }
  39. }
  40.  
  41. fn bar(&mut self)
  42. {
  43. self.b1 = String::from("Foo Bar");
  44. }
  45. }
  46.  
  47. fn main()
  48. {
  49. let mut b = B::new();
  50. let a = A::new(&mut b); // Trying to avoid making a mutable.
  51. let a = a.foo();
  52.  
  53. println!("{:?}", &a);
  54. }
Add Comment
Please, Sign In to add comment