From 84fe604d3492420821e7afc0772c6d2fb4c2e046 Mon Sep 17 00:00:00 2001 From: hybrid Date: Sat, 3 Oct 2026 13:14:53 +0300 Subject: upd: rectangles --- src/rectangles/src/main.rs | 36 ++++++++++++++++++++++++++++++++---- 1 file 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)); } -- cgit v1.3.1