Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. macro_rules! impl_xy {
  2. // `()` indicates that the macro takes no argument.
  3. ($t:ty) => (
  4. impl Point for $t {
  5. fn x(&self) -> f64 {
  6. self.x
  7. }
  8. fn y(&self) -> f64 {
  9. self.y
  10. }
  11. }
  12. )
  13. }
  14.  
  15. pub trait Point {
  16. fn x(&self) -> f64;
  17. fn y(&self) -> f64;
  18. }
  19.  
  20. struct A {
  21. x: f64,
  22. y: f64,
  23. }
  24.  
  25. impl_xy!(A);
  26.  
  27. struct B {
  28. x: f64,
  29. y: f64,
  30. z: f64,
  31. }
  32. impl_xy!(B);
  33.  
  34.  
  35. fn get_x<T: Point>(p: &T) -> f64 {
  36. p.x()
  37. }
  38.  
  39. fn main() {
  40. let a = A { x: 1.0, y: 2.0 };
  41. let b = B { x: 3.0, y: 4.0, z: 5.0 };
  42.  
  43. println!("{}, {}", get_x(&a), get_x(&b));
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement