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

[Josh] Week 3 solution #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
68 changes: 68 additions & 0 deletions week3/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions week3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.8.5"
84 changes: 84 additions & 0 deletions week3/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,85 @@
use rand::prelude::*;
use std::f64::consts::PI;

#[derive(Debug)]
pub enum Shape {
Circle(f64),
Square(isize),
Rectangle(isize, isize),
}

pub fn area(shape: &Shape) -> f64 {
match shape {
Shape::Circle(radius) => radius * radius * PI,
Shape::Square(length) => (length * length) as f64,
Shape::Rectangle(width, height) => (height * width) as f64,
Comment on lines +14 to +15
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not too sure about the types here. Is there a better way to handle this?

}
}

pub fn random_shape() -> Shape {
let mut rng = rand::thread_rng();
let random_number: isize = rng.gen_range(0..2);

match random_number {
0 => Shape::Circle(rng.gen()),
1 => Shape::Rectangle(rng.gen(), rng.gen()),
_ => Shape::Square(rng.gen()),
}
}

pub fn circle_area(shape: &Shape) -> Option<f64> {
if let Shape::Circle(shape) = shape {
Some(area(&Shape::Circle(*shape)))
} else {
None
}
}

#[cfg(test)]
mod test_shape_areas {
use super::*;

#[test]
fn gets_area_of_circle() {
const RADIUS: f64 = 1.0;
const ANSWER: f64 = 3.141592653589793;

let circle = Shape::Circle(RADIUS);

assert_eq!(ANSWER, area(&circle));
}
#[test]
fn gets_area_of_rectangle() {
const WIDTH: isize = 100;
const HEIGHT: isize = 20;
const ANSWER: f64 = 2000.0 as f64;
let rectangle = Shape::Rectangle(WIDTH, HEIGHT);

assert_eq!(ANSWER, area(&rectangle));
}
#[test]
fn gets_area_of_square() {
const LENGTH: isize = 100;
const ANSWER: f64 = 10000.0 as f64;
let square = Shape::Square(LENGTH);

assert_eq!(ANSWER, area(&square));
}
#[test]
fn gets_circle_area() {
const RADIUS: f64 = 1.0;
const ANSWER: Option<f64> = Some(3.141592653589793);

let circle = Shape::Circle(RADIUS);

assert_eq!(ANSWER, circle_area(&circle));
}
#[test]
fn gets_circle_area_else() {
const LENGTH: isize = 100;

let square = Shape::Square(LENGTH);

assert_eq!(None, circle_area(&square));
}
}
6 changes: 6 additions & 0 deletions week3/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use week3::random_shape;

fn main() {
let shape = random_shape();
println!("{:?}", shape);
}