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
|
#[derive(Debug)]
struct Rect {
height: u32,
width: u32,
}
impl Rect {
fn new(size: u32) -> Self {
Self {
height: size,
width: size,
}
}
fn area(&self) -> u32 {
self.width * self.height
}
fn can_hold(&self, r2: &Rect) -> bool {
if self.height <= r2.width && self.width <= r2.width {
return false;
} else {
return true;
}
}
}
fn main() {
let r = Rect {
height: 50,
width: 30,
};
println!("The area of the rectangle is {} square pixels.", r.area());
println!("The r = {:?}",r);
dbg!(&r); // return ownership
let r2 = Rect {
height: 40,
width: 10,
};
let r3 = Rect {
height: 45,
width: 60,
};
println!("Can rect1 hold rect2? {}", r.can_hold(&r2));
println!("Can rect1 hold rect3? {}", r.can_hold(&r3));
let r4 = Rect::new(40);
println!("{:?}", r4);
}
|