Advertisement
Guest User

Untitled

a guest
May 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. //#[derive(Debug)]
  2. struct Foo {
  3. bar: Bar,
  4. }
  5.  
  6. impl Foo {
  7. fn inc2(&mut self) -> i32 {
  8. dbg!("in Foo::inc2");
  9. self.v += 10;
  10. self.v
  11. }
  12. }
  13.  
  14. impl std::ops::Deref for Foo {
  15. type Target = Bar;
  16.  
  17. fn deref(&self) -> &Self::Target {
  18. &self.bar
  19. }
  20. }
  21.  
  22. impl std::ops::DerefMut for Foo {
  23. fn deref_mut(&mut self) -> &mut Bar {
  24. &mut self.bar
  25. }
  26. }
  27.  
  28. #[derive(Debug,Clone)]
  29. struct Bar {
  30. v: i32,
  31. }
  32.  
  33. impl Bar {
  34. fn inc(&mut self) -> i32 {
  35. dbg!("in Bar::inc");
  36. self.v += 1;
  37. self.v
  38. }
  39. }
  40.  
  41. fn main () {
  42. let mut x = Bar {v: 5};
  43. dbg!(&x);
  44. x.inc();
  45. dbg!(x.inc());
  46. dbg!(x.inc());
  47. dbg!(x.clone());
  48.  
  49. let mut y = Foo { bar: x };
  50. //let y = &mut y;
  51. y.inc2();
  52. y.inc();
  53. dbg!(&y as &Bar);
  54. dbg!(y.clone());
  55. dbg!(&*y);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement