summaryrefslogtreecommitdiffstats
path: root/examples/13_trait.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2014-07-02 02:54:21 -0400
committerJesse Luehrs <doy@tozt.net>2014-07-02 02:54:21 -0400
commite620daf3b1a40ae825c625bc4328fb7d2b3ee8b6 (patch)
treefaa469408ee78623eaa8d2720dcbb36096788ca1 /examples/13_trait.rs
downloadintro-to-rust-yapc-na-2014-e620daf3b1a40ae825c625bc4328fb7d2b3ee8b6.tar.gz
intro-to-rust-yapc-na-2014-e620daf3b1a40ae825c625bc4328fb7d2b3ee8b6.zip
initial commitHEADmaster
Diffstat (limited to 'examples/13_trait.rs')
-rw-r--r--examples/13_trait.rs24
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 });
+}
+