some more stuff

This commit is contained in:
Jasper Ras 2025-08-21 21:48:08 +02:00
parent cc2f1b67a8
commit eb9773627e
12 changed files with 150 additions and 0 deletions

7
ch5/rectangles/Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "rectangles"
version = "0.1.0"

View file

@ -0,0 +1,6 @@
[package]
name = "rectangles"
version = "0.1.0"
edition = "2024"
[dependencies]

View file

@ -0,0 +1,37 @@
#[derive(Debug)]
struct Rect {
width: u32,
height: u32,
}
impl Rect {
fn area(&self) -> u32 {
self.width * self.height
}
fn can_hold(&self, other: &Rect) -> bool {
self.width >= other.width && self.height >= other.height
}
}
fn main() {
let rect1 = Rect {
width: 30,
height: 50,
};
let rect2 = Rect {
width: 29,
height: 50,
};
let rect3 = Rect {
width: 29,
height: 51,
};
// println!("The area is {rect1:#?}");
println!("{} * {} = {}", rect1.width, rect1.height, rect1.area());
println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3));
}