-
Notifications
You must be signed in to change notification settings - Fork 0
/
5-2.rs
51 lines (42 loc) · 833 Bytes
/
5-2.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
struct Rectangle1<T> {
width: T,
height: T,
}
struct Rectangle2<T, U> {
width: T,
height: U,
}
impl<T> Rectangle1<T> {
fn width(&self) -> &T {
&self.width
}
fn height(&self) -> &T {
&self.height
}
}
impl Rectangle1<i32> {
fn area(&self) -> i32 {
self.width * self.height
}
}
impl<T, C> Rectangle2<T, C> {
fn width(&self) -> &T {
&self.width
}
fn height(&self) -> &C {
&self.height
}
}
fn main() {
let rect1 = Rectangle1 {
width: 8,
height: 2,
};
println!("width:{} height:{}", rect1.width(), rect1.height());
println!("rect1 area : {}", rect1.area());
let rect2 = Rectangle2 {
width: 8,
height: 2.2,
};
println!("width:{} height:{}", rect2.width(), rect2.height());
}