]> git.lizzy.rs Git - rust.git/blob - src/test/ui/binop/operator-multidispatch.rs
:arrow_up: rust-analyzer
[rust.git] / src / test / ui / binop / operator-multidispatch.rs
1 // run-pass
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
7 #[derive(Debug,PartialEq,Eq)]
8 struct Point {
9     x: isize,
10     y: isize
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
21 impl ops::Add<isize> for Point {
22     type Output = Point;
23
24     fn add(self, other: isize) -> Point {
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 }