Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

[Josh] Week 2 solution #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 {
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;

println!("The width has now changed and is {}", rect1.width);

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

let rect3: Rectangle = Rectangle {
width: 999,
height: 999,
};

print!("Can rect1 hold rect2? {}! ", rect1.can_hold(rect2));
print!("Can rect1 hold rect3? {}! ", rect1.can_hold(rect3));
}