-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrait_simple_vs_enum.rs
55 lines (44 loc) · 1.22 KB
/
trait_simple_vs_enum.rs
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
44
45
46
47
48
49
50
51
52
53
54
55
// 1. Simple trait example
trait TShape {
fn area(&self) -> f64;
}
struct Rectangle { height: f64, width: f64 }
impl TShape for Rectangle {
fn area(&self) -> f64 {
self.width * self.height
}
}
struct Circle { radius: f64 }
impl TShape for Circle {
fn area(&self) -> f64 {
std::f64::consts::PI * self.radius * self.radius
}
}
#[test]
fn ex1_simple_trait_example() {
let rec = Rectangle { width: 4.0, height: 3.0};
assert_eq!(rec.area(), 12.0);
let cir = Circle { radius: 3.0};
assert_eq!(cir.area(), 28.274333882308138);
}
// 2. impl enum also kind of inheritance
// src: https://www.lurklurk.org/effective-rust/use-types-2.html
enum Shape {
Rectangle { width: f64, height: f64 },
Circle { radius: f64 },
}
impl Shape {
pub fn area(&self) -> f64 {
match self {
Shape::Rectangle { width, height } => width * height,
Shape::Circle { radius } => std::f64::consts::PI * radius * radius,
}
}
}
#[test]
fn ex2_impl_enum_also_kind_of_inheritance() {
let rec = Shape::Rectangle { width: 4.0, height: 3.0};
assert_eq!(rec.area(), 12.0);
let cir = Shape::Circle { radius: 3.0};
assert_eq!(cir.area(), 28.274333882308138);
}