Skip to content

Instantly share code, notes, and snippets.

@greyblake
Created August 28, 2016 19:27
Show Gist options
  • Save greyblake/6642ce91662793cc2fc33cf18bb00bda to your computer and use it in GitHub Desktop.
Save greyblake/6642ce91662793cc2fc33cf18bb00bda to your computer and use it in GitHub Desktop.
#[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