Last active
August 28, 2016 15:01
-
-
Save greyblake/e06c3274c02bf87ab37acbf82ef8b647 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
trait HasArea { | |
fn area(&self) -> f64; | |
} | |
struct Cyrcle { | |
radius : f64, | |
} | |
impl HasArea for Cyrcle { | |
fn area(&self) -> f64 { | |
std::f64::consts::PI * self.radius.powi(2) | |
} | |
} | |
struct Square { | |
a : f64, | |
} | |
impl HasArea for Square { | |
fn area(&self) -> f64 { | |
self.a.powi(2) | |
} | |
} | |
struct Rectangle { | |
a : f64, | |
b : f64, | |
} | |
impl HasArea for Rectangle { | |
fn area(&self) -> f64 { | |
self.a * self.b | |
} | |
} | |
fn print_area<T : HasArea>(shape : &T) { | |
println!("Shape: {}", shape.area()); | |
} | |
fn main() { | |
let cyrcle = Cyrcle { radius: 10.0 }; | |
let square = Square { a: 5.0 }; | |
let rectangle = Rectangle { a: 4.0, b: 10.0 }; | |
print_area(&cyrcle); | |
print_area(&square); | |
print_area(&rectangle); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment