main
1#[derive(Debug)]
2struct Rectangle {
3 width: u32,
4 height: u32,
5}
6
7impl Rectangle {
8 fn area(&self) -> u32 {
9 self.width * self.height
10 }
11
12 fn can_hold(&self, other: &Rectangle) -> bool {
13 self.width > other.width && self.height > other.height
14 }
15
16 fn square(size: u32) -> Self {
17 Self {
18 width: size,
19 height: size,
20 }
21 }
22}
23
24fn main() {
25 let rect1 = Rectangle {
26 width: 30,
27 height: 50,
28 };
29 let rect2 = Rectangle {
30 width: 10,
31 height: 40,
32 };
33 let rect3 = Rectangle {
34 width: 60,
35 height: 45,
36 };
37 let rect4 = Rectangle::square(10);
38
39 println!(
40 "The area of the rectangle is {} square pixels.",
41 rect1.area()
42 );
43 println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
44 println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3));
45 println!("rect4 is a square: {}", &rect4.area());
46}