Skip to content

Instantly share code, notes, and snippets.

@greyblake
Created August 28, 2016 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greyblake/73e5968e26c5e7108be2cf68a48e2f03 to your computer and use it in GitHub Desktop.
Save greyblake/73e5968e26c5e7108be2cf68a48e2f03 to your computer and use it in GitHub Desktop.
#[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