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