-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnums_Vec_Usage.rs
More file actions
43 lines (38 loc) · 1.19 KB
/
Enums_Vec_Usage.rs
File metadata and controls
43 lines (38 loc) · 1.19 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
Trying to implment usecases of Enum and vector
*/
#[derive(Debug)]
enum Shape {
Circle(f64),
Square(f64),
Triangle(f64,f64,f64),
}
fn max_shape(v: Vec<Shape>) -> Shape {
let mut mv : f64 = 0.0;
let mut rs : Shape = Shape::Circle(0.0);
for val in v {
let t : f64 = match val {
Shape::Circle(radius) => std::f64::consts::PI * radius * radius,
Shape::Square(length) => length * length,
Shape::Triangle(a, b, c) => { let s = (a+b+c)/2.0; f64::sqrt(s*(s-a)*(s-b)*(s-c)) }
};
if mv < t {
mv = t;
rs = val;
}
};
rs
}
fn main() {
let shapes = vec![Shape::Circle(5.0), Shape::Square(3.0), Shape::Triangle(8.0,4.0,5.0)];
let total_area: f64 = shapes
.iter()
.map(|shape| match shape {
Shape::Circle(radius) => std::f64::consts::PI * radius * radius,
Shape::Square(length) => length * length,
Shape::Triangle(a, b, c) => { let s = (a+b+c)/2.0; f64::sqrt(s*(s-a)*(s-b)*(s-c)) }
})
.sum();
println!("Total area: {} sq. units", total_area);
println!("Shape with the highest area {:?}", max_shape(shapes));
}