Created
August 28, 2016 19:27
-
-
Save greyblake/6642ce91662793cc2fc33cf18bb00bda to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug)] | |
struct Point { | |
x : f64, | |
} | |
trait Add { | |
fn add(&self, &Self) -> Self; | |
} | |
trait Sub { | |
fn sub(&self, &Self) -> Self; | |
} | |
trait Math : Add + Sub { | |
} | |
impl Add for Point { | |
fn add(&self, another : &Point) -> Point { | |
Point { x: self.x + another.x } | |
} | |
} | |
impl Sub for Point { | |
fn sub(&self, another : &Point) -> Point { | |
Point { x: self.x - another.x } | |
} | |
} | |
impl Math for Point { | |
} | |
fn print_math<T>(a : T, b : T) where T: Math + std::fmt::Debug { | |
println!("{:?} + {:?} = {:?}", a, b, a.add(&b)); | |
println!("{:?} - {:?} = {:?}", a, b, a.sub(&b)); | |
} | |
fn main() { | |
let p1 = Point { x: 1.0 }; | |
let p2 = Point { x: 0.3 }; | |
println!("{:?}", p1); | |
print_math(p1, p2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment