summaryrefslogtreecommitdiff
path: root/5.3/src/main.rs
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-06-03 11:38:11 -0600
committermo khan <mo@mokhan.ca>2025-06-03 11:38:11 -0600
commit456eb97609267215e31215ab91277808ab54127a (patch)
tree8c4563f0f8cf039d46f3571402e2dbd520ecb5c6 /5.3/src/main.rs
parent11e7afe4c3d2aed43d50aae838be88e905876d36 (diff)
create method the references another instance of the same type
Diffstat (limited to '5.3/src/main.rs')
-rw-r--r--5.3/src/main.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/5.3/src/main.rs b/5.3/src/main.rs
index f0d455d..9d12566 100644
--- a/5.3/src/main.rs
+++ b/5.3/src/main.rs
@@ -8,6 +8,10 @@ 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 main() {
@@ -15,8 +19,19 @@ fn main() {
width: 30,
height: 50,
};
+ let rect2 = Rectangle {
+ width: 10,
+ height: 40,
+ };
+ let rect3 = Rectangle {
+ width: 60,
+ height: 45,
+ };
+
println!(
"The area of the rectangle is {} square pixels.",
rect1.area()
);
+ println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
+ println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3));
}