Skip to content

Commit c9165d4

Browse files
Defining and using structs
1 parent 6031064 commit c9165d4

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

rustlang_book/structs/src/main.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
1+
#[derive(Debug)]
2+
struct Rectangle {
3+
width: u32,
4+
height: u32,
5+
}
6+
7+
impl Rectangle {
8+
fn area(&self) -> u32 {
9+
return self.height * self.width;
10+
}
11+
12+
fn can_hold(&self, rectangle: &Rectangle) -> bool {
13+
return self.height > rectangle.height && self.width > rectangle.width;
14+
}
15+
}
116

217
fn main() {
3-
println!("Hello, world!");
18+
let rect1 = Rectangle {
19+
width: 30,
20+
height: 50,
21+
};
22+
let rect2 = Rectangle {
23+
width: 10,
24+
height: 40,
25+
};
26+
let rect3 = Rectangle {
27+
width: 60,
28+
height: 45,
29+
};
30+
31+
println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
32+
println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3));
433
}

0 commit comments

Comments
 (0)