diff options
Diffstat (limited to 'examples/13_trait.rs')
-rw-r--r-- | examples/13_trait.rs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/examples/13_trait.rs b/examples/13_trait.rs new file mode 100644 index 0000000..8976d0a --- /dev/null +++ b/examples/13_trait.rs @@ -0,0 +1,24 @@ +trait Area { + fn area (&self) -> f64; +} +struct Rectangle { + width: f64, + height: f64, +} +impl Area for Rectangle { + fn area (&self) -> f64 { self.width * self.height } +} +struct Circle { + radius: f64, +} +impl Area for Circle { + fn area (&self) -> f64 { 3.14 * self.radius * self.radius } +} +fn print_area<T: Area> (shape: T) { + println!("{}", shape.area()); +} +fn main () { + print_area(Circle { radius: 2.0 }); + print_area(Rectangle { width: 3.2, height: 4.5 }); +} + |