trait Shape {
fn area(&self) -> f64;
}
struct Circle {
radius: f64,
}
impl Shape for Circle {
fn area(&self) -> f64 {
std::f64::consts::PI * self.radius * self.radius
}
}
struct Rectangle {
width: u32,
height: u32,
}
impl Shape for Rectangle {
fn area(&self) -> f64 {
(self.width * self.height) as f64
}
}
fn main() {
let mut shapes: Vec<Box<dyn Shape>> = Vec::new();
shapes.push(Box::new(Circle { radius: 5.0 }));
shapes.push(Box::new(Rectangle { width: 10, height: 5 }));
for shape in shapes {
println!("Area: {}", shape.area());
}
}