Created
August 28, 2016 19:35
-
-
Save greyblake/73e5968e26c5e7108be2cf68a48e2f03 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 Math : { | |
fn sub(&self, &Self) -> Self; | |
fn add(&self, &Self) -> Self; | |
} | |
impl Math for Point { | |
fn add(&self, another : &Point) -> Point { | |
Point { x: self.x + another.x } | |
} | |
fn sub(&self, another : &Point) -> Point { | |
Point { x: self.x - another.x } | |
} | |
} | |
impl Drop for Point { | |
fn drop(&mut self) { | |
println!("Chao, x = {}", self.x); | |
} | |
} | |
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 }; | |
print_math(p1, p2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment