rectangles: implement area method

This commit is contained in:
laurens 2020-05-22 14:06:55 +02:00
parent e9f349d55c
commit e011a4f94f

View file

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