-
Notifications
You must be signed in to change notification settings - Fork 0
/
5-7.rs
44 lines (37 loc) · 809 Bytes
/
5-7.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
use std::fmt::{Display, Formatter, Result};
trait Geometry {
fn area(&self) -> f32;
fn perimeter(&self) -> f32;
}
struct Rectangle {
width: f32,
height: f32,
}
impl Display for Rectangle {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "Rectangle : ( {}, {} )", self.width, self.height)
}
}
impl Geometry for Rectangle {
fn area(&self) -> f32 {
self.width * self.height
}
fn perimeter(&self) -> f32 {
(self.width + self.height) * 2.0
}
}
fn print(geometry: impl Geometry + Display) {
println!(
"{}, area:{}, perimeter:{}",
geometry.fmt,
geometry.area(),
geometry.perimeter()
);
}
fn main() {
let rect = Rectangle {
width: 3.0,
height: 5.9,
};
print(rect);
}