Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- macro_rules! impl_xy {
- // `()` indicates that the macro takes no argument.
- ($t:ty) => (
- impl Point for $t {
- fn x(&self) -> f64 {
- self.x
- }
- fn y(&self) -> f64 {
- self.y
- }
- }
- )
- }
- pub trait Point {
- fn x(&self) -> f64;
- fn y(&self) -> f64;
- }
- struct A {
- x: f64,
- y: f64,
- }
- impl_xy!(A);
- struct B {
- x: f64,
- y: f64,
- z: f64,
- }
- impl_xy!(B);
- fn get_x<T: Point>(p: &T) -> f64 {
- p.x()
- }
- fn main() {
- let a = A { x: 1.0, y: 2.0 };
- let b = B { x: 3.0, y: 4.0, z: 5.0 };
- println!("{}, {}", get_x(&a), get_x(&b));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement