rectangles: add can_hold function

This commit is contained in:
laurens 2020-05-22 14:08:20 +02:00
parent e011a4f94f
commit 0856d0c080

View file

@ -8,6 +8,10 @@ impl Rectangle {
fn area(&self) -> u32 { fn area(&self) -> u32 {
self.width * self.height self.width * self.height
} }
fn can_hold(&self, other: &Rectangle) -> bool {
self.width > other.width && self.height > other.height
}
} }
fn main() { fn main() {
@ -40,6 +44,12 @@ fn main() {
"The area of the rectangle is {} square pixels.", "The area of the rectangle is {} square pixels.",
rect.area() rect.area()
); );
println!(
"The rectangle {:#?} can hold itself obviously: {}.",
rect,
rect.can_hold(&rect),
);
} }
fn area(width: u32, height: u32) -> u32 { fn area(width: u32, height: u32) -> u32 {