some more stuff
This commit is contained in:
parent
cc2f1b67a8
commit
eb9773627e
12 changed files with 150 additions and 0 deletions
37
ch5/rectangles/src/main.rs
Normal file
37
ch5/rectangles/src/main.rs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#[derive(Debug)]
|
||||
struct Rect {
|
||||
width: u32,
|
||||
height: u32,
|
||||
}
|
||||
|
||||
impl Rect {
|
||||
fn area(&self) -> u32 {
|
||||
self.width * self.height
|
||||
}
|
||||
|
||||
fn can_hold(&self, other: &Rect) -> bool {
|
||||
self.width >= other.width && self.height >= other.height
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let rect1 = Rect {
|
||||
width: 30,
|
||||
height: 50,
|
||||
};
|
||||
|
||||
let rect2 = Rect {
|
||||
width: 29,
|
||||
height: 50,
|
||||
};
|
||||
|
||||
let rect3 = Rect {
|
||||
width: 29,
|
||||
height: 51,
|
||||
};
|
||||
|
||||
// println!("The area is {rect1:#?}");
|
||||
println!("{} * {} = {}", rect1.width, rect1.height, rect1.area());
|
||||
println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
|
||||
println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue