Skip to content
This repository was archived by the owner on Mar 7, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions week2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,42 @@ struct Rectangle {
pub height: u32,
}

// Calculate the area of a rectangle, given its width and height.
fn area(rectangle: Rectangle) -> u32 {
todo!()
impl Rectangle {
// Calculate the area of a rectangle, given its width and height.
fn area(&self) -> u32 {
self.width * self.height
}

fn can_hold(&self, other: &Rectangle) -> bool {
self.width > other.width && self.height > other.height
}
}

fn main() {
// Create a new instance of the `Rectangle` struct, and give it a width of 30 and a height of 50.
let rect1 = Rectangle {
let mut rect1 = Rectangle {
width: 30,
height: 50,
};

// Print out the area of the rectangle.
println!(
"The area of the rectangle is {} square pixels.",
area(rect1)
rect1.area()
);

rect1.width = 100;

// Print out the area of the rectangle.
println!(
"The area of the rectangle is {} square pixels.",
rect1.area()
);

let rect2 = Rectangle {
width: 5,
height: 5,
};

println!("Rect 1 can hold rect2: {}.", rect1.can_hold(&rect2));
}