rectangles: Add associated function
This commit is contained in:
parent
0856d0c080
commit
99d13b5a2f
1 changed files with 17 additions and 1 deletions
|
|
@ -5,6 +5,14 @@ struct Rectangle {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rectangle {
|
impl Rectangle {
|
||||||
|
fn build_rectangle(width: u32, height: u32) -> Rectangle {
|
||||||
|
Rectangle { width, height }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn square(size: u32) -> Rectangle {
|
||||||
|
Rectangle::build_rectangle(size, size)
|
||||||
|
}
|
||||||
|
|
||||||
fn area(&self) -> u32 {
|
fn area(&self) -> u32 {
|
||||||
self.width * self.height
|
self.width * self.height
|
||||||
}
|
}
|
||||||
|
|
@ -48,7 +56,15 @@ fn main() {
|
||||||
println!(
|
println!(
|
||||||
"The rectangle {:#?} can hold itself obviously: {}.",
|
"The rectangle {:#?} can hold itself obviously: {}.",
|
||||||
rect,
|
rect,
|
||||||
rect.can_hold(&rect),
|
rect.can_hold(&rect),
|
||||||
|
);
|
||||||
|
|
||||||
|
let square = Rectangle::square(3);
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"The area of the square rectangle {:#?} is {} square pixels.",
|
||||||
|
square,
|
||||||
|
square.area()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue