Guest User

Untitled

a guest
Jul 21st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. trait X {
  2. fn x(&self) -> i32;
  3. }
  4. trait Y {
  5. fn y(&self) -> i32;
  6. }
  7.  
  8. // question: i have implemented X for Y, so
  9. // shouldn't this mean all instances of Y are
  10. // also X?
  11. impl X for Y {
  12. fn x(&self) -> i32 {
  13. 123
  14. }
  15. }
  16.  
  17. // question: here i have a struct Z, and I
  18. // implement Y for Z, and Y implements X, so
  19. // shouldn't X by implemented for Z by way of
  20. // Y?
  21. struct Z;
  22. impl Y for Z {
  23. fn y(&self) -> i32 {
  24. 0
  25. }
  26. }
  27.  
  28. fn test(y: Box<dyn Y>) {
  29. println!("{}", y.x());
  30. }
  31.  
  32. fn main() {
  33. let z = Box::new(Z);
  34. test(z);
  35.  
  36. }
Add Comment
Please, Sign In to add comment