From 9f25a0ae350bac8b03a168e71882fea2fd183e30 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Thu, 28 Nov 2024 00:12:32 +0100 Subject: [PATCH] Add a functions for creating squares --- rectangles/src/main.rs | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/rectangles/src/main.rs b/rectangles/src/main.rs index a0825bc..8050301 100644 --- a/rectangles/src/main.rs +++ b/rectangles/src/main.rs @@ -5,29 +5,14 @@ struct Rectangle { } 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 rect1 = Rectangle { - width: 30, - height: 50, - }; - let rect2 = Rectangle { - width: 10, - height: 40, - }; - let rect3 = Rectangle { - width: 60, - height: 45, - }; - - println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2)); - println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3)); + let sq = Rectangle::square(3); }