Rect area as method

cat-town
Dan Buch 6 years ago
parent 3211792108
commit 154344fc1d
Signed by: meatballhat
GPG Key ID: 9685130D8B763EA7

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

Loading…
Cancel
Save