summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/rectangles/src/main.rs36
1 files changed, 32 insertions, 4 deletions
diff --git a/src/rectangles/src/main.rs b/src/rectangles/src/main.rs
index fec8596..b6a1dc4 100644
--- a/src/rectangles/src/main.rs
+++ b/src/rectangles/src/main.rs
@@ -4,21 +4,49 @@ struct Rectangle {
height: u32,
}
+impl Rectangle {
+ fn area(&self) -> u32 {
+ self.width * self.height
+ }
+
+ fn can_hold(&self, other: &Rectangle) -> bool {
+ self.width > other.width && self.height > other.height
+ }
+
+ fn square(size: u32) -> Self {
+ Self {
+ width: size,
+ height: size,
+ }
+ }
+}
+
fn main() {
let scale = 2;
let rect1 = Rectangle {
width: dbg!(30 * scale),
height: 50,
};
+ let rect2 = Rectangle {
+ width: 10,
+ height: 40,
+ };
+ let rect3 = Rectangle {
+ width: 60,
+ height: 45,
+ };
+ let rect4 = Rectangle::square(10);
dbg!(&rect1);
+ dbg!(&rect2);
+ dbg!(&rect3);
+ dbg!(&rect4);
println!(
"The area of the rectangle is {} square pixels.",
- area(&rect1)
+ rect1.area()
);
-}
-fn area(rectangle: &Rectangle) -> u32 {
- rectangle.width * rectangle.height
+ println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
+ println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3));
}