Last active
August 29, 2015 14:16
-
-
Save alanjcfs/0001cba2ea6de3f43943 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub mod area_calculator { | |
#[test] | |
fn it_works() { | |
let s = Rectangle{width: 10u32, height: 20u32}; | |
assert!(s.area() == 200u32); | |
let t = Square{ side: 20u32 }; | |
assert!(t.area() == 400u32); | |
} | |
pub struct Rectangle { | |
width: u32, | |
height: u32, | |
} | |
pub struct Square { | |
side: u32 | |
} | |
struct Triangle { | |
height: u32, | |
base: u32 | |
} | |
trait Area { | |
fn area(&self) -> u32; | |
} | |
impl Square { | |
fn side(&self) -> u32 { | |
self.side | |
} | |
} | |
impl Area for Square { | |
fn area(&self) -> u32 { | |
self.side * self.side | |
} | |
} | |
impl Rectangle { | |
fn width(&self) -> u32 { | |
self.width | |
} | |
fn height(&self) -> u32 { | |
self.height | |
} | |
} | |
impl Area for Rectangle { | |
fn area(&self) -> u32 { | |
self.width() * self.height() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment