1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| enum Shape {
Rectangle { width:f32, height:f32 },
Triangle { side:f32},
Circ { radius:f32 },
}
impl Shape {
pub fn perimeter(&self) -> f32 {
match self {
Shape::Rectangle { width, height } => width*2.0+height*2.0,
Shape::Triangle { side } => side*3.0,
Shape::Circ { radius } => 2.0*std::f32::consts::PI*radius,
}
}
pub fn area(&self) -> f32 {
match self {
Shape::Rectangle { width, height } => width*height,
Shape::Triangle { side } => side*0.5*3.0_f32.sqrt(),
Shape::Circ { radius } => std::f32::consts::PI*radius*radius,
}
}
}
fn main() {
let rectangle = Shape::Rectangle { width:1.0, height:2.0 };
let triangle = Shape::Triangle { side:1.0 };
let cric = Shape::Circ { radius:1.0 };
dbg!(rectangle.perimeter(),rectangle.area());
dbg!(triangle.perimeter(),triangle.area());
dbg!(cric.perimeter(),cric.area());
}
|