From 38ac951a8383c072e8913d30c1dc8564729d9cb6 Mon Sep 17 00:00:00 2001 From: Jon Janzen Date: Tue, 3 Nov 2020 16:12:43 -0700 Subject: [PATCH] O --- src/main.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index e7a11a9..1b898a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,45 @@ -fn main() { - println!("Hello, world!"); +use std::ops::Add; + +#[derive(Debug, PartialEq)] +struct Point { + x: i32, + y: i32, +} + +impl Add for Point { + type Output = Point; + + fn add(self, other: Point) -> Point { + Point { + x: self.x + other.x, + y: self.y + other.y, + } + } +} + + +#[derive(Debug)] +struct Millimeters(u32); +#[derive(Debug)] +struct Meters(u32); + +impl Add for Millimeters { + type Output = Millimeters; + + fn add(self, other: Meters) -> Millimeters { + Millimeters(self.0 + (other.0 * 1000)) + } +} + +fn main() { + assert_eq!( + Point { x: 1, y: 0 } + Point { x: 2, y: 3 }, + Point { x: 3, y: 3 } + ); + + assert_eq!( + Millimeters(1001), + Millimeters(1) + Meters(1) + ) + }