Struct method woop

This commit is contained in:
Dan Buch 2020-11-28 17:18:42 -05:00
parent d64444550a
commit 1622e4bcea
Signed by: meatballhat
GPG Key ID: 9685130D8B763EA7

View File

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