Rect area as method

This commit is contained in:
Dan Buch 2017-12-31 10:30:54 -05:00
parent 3211792108
commit 154344fc1d
Signed by: meatballhat
GPG Key ID: 9685130D8B763EA7

View File

@ -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
}