Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #[derive(Debug, Clone)]
  2. struct Point {
  3. x: i32,
  4. y: i32
  5. }
  6.  
  7. impl std::ops::Add<Point> for Point {
  8. type Output = Point;
  9.  
  10. fn add( self, other: Point ) -> Point {
  11. Point { x: self.x + other.x, y: self.y + other.y }
  12. }
  13. }
  14.  
  15. impl std::ops::Add<Box<Point>> for Point {
  16. type Output = Point;
  17.  
  18. fn add( self, other: Box<Point> ) -> Point {
  19. self + *other
  20. }
  21. }
  22.  
  23. impl std::ops::Add<Point> for Box<Point> {
  24. type Output = Point;
  25.  
  26. fn add( self, other: Point ) -> Point {
  27. *self + other
  28. }
  29. }
  30.  
  31. impl std::ops::Add<Box<Point>> for Box<Point> {
  32. type Output = Point;
  33.  
  34. fn add( self, other: Box<Point> ) -> Point {
  35. *self + *other
  36. }
  37. }
  38.  
  39. fn main() {
  40. let first_point: Point = Point { x: 1, y: 2 };
  41. let second_point: Box<Point> = Box::new( Point { x: 1, y: 1 } );
  42. let third_point: Box<Point> = second_point.clone();
  43. let fourth_point: Point = first_point + (second_point + third_point);
  44.  
  45. println!( "{:?}", fourth_point );
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement