Implement area using a method

This commit is contained in:
Manuel Thalmann 2024-11-27 23:39:46 +01:00
parent d9963ced18
commit a8248ed120

View file

@ -4,12 +4,20 @@ struct Rectangle {
height: u32, height: u32,
} }
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}
fn main() { fn main() {
let scale = 2;
let rect1 = Rectangle { let rect1 = Rectangle {
width: dbg!(30 * scale), width: 30,
height: 50, height: 50,
}; };
dbg!(&rect1); println!(
"The area of the rectangle is {} square pixels.",
rect1.area()
);
} }