]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Test that we can overload the `+` operator for points so that two |
2 | // points can be added, and a point can be added to an integer. | |
3 | ||
4 | use std::ops; | |
5 | ||
85aaf69f | 6 | #[derive(Debug,PartialEq,Eq)] |
1a4d82fc | 7 | struct Point { |
c34b1796 AL |
8 | x: isize, |
9 | y: isize | |
1a4d82fc JJ |
10 | } |
11 | ||
12 | impl ops::Add for Point { | |
13 | type Output = Point; | |
14 | ||
15 | fn add(self, other: Point) -> Point { | |
16 | Point {x: self.x + other.x, y: self.y + other.y} | |
17 | } | |
18 | } | |
19 | ||
c34b1796 | 20 | impl ops::Add<isize> for Point { |
1a4d82fc JJ |
21 | type Output = Point; |
22 | ||
c34b1796 | 23 | fn add(self, other: isize) -> Point { |
1a4d82fc JJ |
24 | Point {x: self.x + other, |
25 | y: self.y + other} | |
26 | } | |
27 | } | |
28 | ||
29 | pub fn main() { | |
30 | let mut p = Point {x: 10, y: 20}; | |
31 | p = p + Point {x: 101, y: 102}; | |
32 | assert_eq!(p, Point {x: 111, y: 122}); | |
33 | p = p + 1; | |
34 | assert_eq!(p, Point {x: 112, y: 123}); | |
35 | } |