From f3c37d8823c482fa005e68fb93975263017f5a8e Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Wed, 22 Jan 2020 22:56:52 +0800 Subject: [PATCH] add mul, div --- ops-test/src/main.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/ops-test/src/main.rs b/ops-test/src/main.rs index 46bf68c..33d5a4f 100644 --- a/ops-test/src/main.rs +++ b/ops-test/src/main.rs @@ -26,13 +26,39 @@ impl std::ops::Sub for Point { } } +impl std::ops::Mul for Point { + type Output = Self; + + fn mul(self, rhs: Self) -> Self { + Point { + x: self.x * rhs.x, + y: self.y * rhs.y, + } + } +} + +impl std::ops::Div for Point { + type Output = Self; + + fn div(self, rhs: Self) -> Self { + Point { + x: self.x / rhs.x, + y: self.y / rhs.y, + } + } +} + fn main() { - let a = Point { x: 1, y: 2, }; - let b = Point { x: 2, y: 4, }; + let a = Point { x: 2, y: 3, }; + let b = Point { x: 4, y: 5, }; let c = a.clone() + b.clone(); let d = a.clone() - b.clone(); + let e = a.clone() * b.clone(); + let f = a.clone() / b.clone(); println!("{:?}", a); println!("{:?}", b); println!("{:?}", c); println!("{:?}", d); + println!("{:?}", e); + println!("{:?}", f); }