-
Notifications
You must be signed in to change notification settings - Fork 0
/
5-5.rs
53 lines (44 loc) · 885 Bytes
/
5-5.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
trait Geometry {
fn area(&self) -> f32;
fn permeter(&self) -> f32;
}
struct Rectangle {
width: f32,
height: f32,
}
impl Geometry for Rectangle {
fn area(&self) -> f32 {
self.width * self.height
}
fn permeter(&self) -> f32 {
(self.width * self.height) * 2.0
}
}
struct Circle {
radius: f32,
}
impl Geometry for Circle {
fn area(&self) -> f32 {
3.14 * self.radius * self.radius
}
fn permeter(&self) -> f32 {
3.14 * 2.0 * self.radius
}
}
fn main() {
let rect = Rectangle {
width: 8.8,
height: 2.2,
};
println!(
"rect.area:{}, rect.permeter:{}",
rect.area(),
rect.permeter()
);
let circle = Circle { radius: 10.2 };
println!(
"circle.area: {}, circle.permeter:{}",
circle.area(),
circle.permeter()
);
}