diff options
| author | hybrid <hybrid@hybridlabs.cc> | 2026-10-03 13:14:53 +0300 |
|---|---|---|
| committer | hybrid <hybrid@hybridlabs.cc> | 2026-10-03 13:14:53 +0300 |
| commit | 84fe604d3492420821e7afc0772c6d2fb4c2e046 (patch) | |
| tree | 52d7bfeffdfe2a789c83944aa3d48a27f14ac889 | |
| parent | 42ac98a58c2d37dc05bcfd32dbc0dff6f231657f (diff) | |
| download | rust-book-84fe604d3492420821e7afc0772c6d2fb4c2e046.tar.gz rust-book-84fe604d3492420821e7afc0772c6d2fb4c2e046.tar.bz2 rust-book-84fe604d3492420821e7afc0772c6d2fb4c2e046.zip | |
upd: rectanglesmain
| -rw-r--r-- | src/rectangles/src/main.rs | 36 |
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)); } |
