Compare commits
3 Commits
595ac0e75b
...
d745026c1a
| Author | SHA1 | Date | |
|---|---|---|---|
|
d745026c1a
|
|||
|
b25fe11bc7
|
|||
|
89fd568b75
|
@@ -14,7 +14,18 @@
|
|||||||
// Once you have completed defining the error type correctly, you should be able to run
|
// Once you have completed defining the error type correctly, you should be able to run
|
||||||
// `cargo build --lib` without any build errors or warnings. Then go to main.rs and continue with #2
|
// `cargo build --lib` without any build errors or warnings. Then go to main.rs and continue with #2
|
||||||
|
|
||||||
// pub enum DolphinError...
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
#[non_exhaustive]
|
||||||
|
pub enum DolphinError {
|
||||||
|
#[error("The dolphin is hungry")]
|
||||||
|
Hungry,
|
||||||
|
#[error("The dolphin is too young")]
|
||||||
|
TooYoung,
|
||||||
|
#[error("The dolphin's name is too long and annoying to say")]
|
||||||
|
LongName
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Dolphin {
|
pub struct Dolphin {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
|||||||
@@ -11,22 +11,26 @@ use aquarium::Dolphin;
|
|||||||
// - Have the play_time function return a `Result<Vec<String>>`. The vector of Strings will
|
// - Have the play_time function return a `Result<Vec<String>>`. The vector of Strings will
|
||||||
// represent successful outcomes of various dolphin tricks.
|
// represent successful outcomes of various dolphin tricks.
|
||||||
|
|
||||||
// fn play_time(dolphin: &Dolphin) -> ... {
|
use anyhow::Result;
|
||||||
// let mut responses = vec![];
|
|
||||||
// // 2b. Call the .say_your_name() method on `dolphin`, use `?` to unwrap the value, and push
|
|
||||||
// // the value onto the `responses` vector.
|
|
||||||
// //
|
|
||||||
// // let response = ... // this can be done with an intermediate variable...
|
|
||||||
// // responses.push( ... ) // ...or all on one line. Either way is fine!
|
|
||||||
// //
|
|
||||||
// // 2c. Do the same thing as #2b for the .flip() method
|
|
||||||
// //
|
|
||||||
// // 2d. Do the same thing as #2b for the .shake_hands() method
|
|
||||||
//
|
|
||||||
// Ok(responses)
|
|
||||||
// }
|
|
||||||
|
|
||||||
fn main() {
|
fn play_time(dolphin: &Dolphin) -> Result<Vec<String>> {
|
||||||
|
let mut responses = vec![];
|
||||||
|
// 2b. Call the .say_your_name() method on `dolphin`, use `?` to unwrap the value, and push
|
||||||
|
// the value onto the `responses` vector.
|
||||||
|
//
|
||||||
|
let response = dolphin.say_your_name()?; // this can be done with an intermediate variable...
|
||||||
|
responses.push(response); // ...or all on one line. Either way is fine!
|
||||||
|
//
|
||||||
|
// 2c. Do the same thing as #2b for the .flip() method
|
||||||
|
responses.push(dolphin.flip()?);
|
||||||
|
//
|
||||||
|
// 2d. Do the same thing as #2b for the .shake_hands() method
|
||||||
|
responses.push(dolphin.shake_hands()?);
|
||||||
|
|
||||||
|
Ok(responses)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
let dolphins = vec![
|
let dolphins = vec![
|
||||||
Dolphin {
|
Dolphin {
|
||||||
name: "Augustinius".into(),
|
name: "Augustinius".into(),
|
||||||
@@ -57,14 +61,23 @@ fn main() {
|
|||||||
// returns an Err variant the first time it is called, the try operator will return it from
|
// returns an Err variant the first time it is called, the try operator will return it from
|
||||||
// main(), which will end the program at the first error. anyhow's Result will take care of
|
// main(), which will end the program at the first error. anyhow's Result will take care of
|
||||||
// formatting the error output for us.
|
// formatting the error output for us.
|
||||||
|
|
||||||
match play_time(dolphin) {
|
match play_time(dolphin) {
|
||||||
Ok(responses) => {
|
Ok(responses) => {
|
||||||
println!("{} did a FABULOUS PERFORMANCE!", dolphin.name);
|
println!("\n{} did a FABULOUS PERFORMANCE!", dolphin.name);
|
||||||
for response in responses {
|
for response in responses {
|
||||||
println!(" {}", response);
|
println!(" {}", response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => println!("{} can't perform today: {}", dolphin.name, e.to_string()),
|
Err(e) => println!("{} can't perform today: {}", dolphin.name, e.to_string()),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// let responses = play_time(dolphin)?;
|
||||||
|
// println!("\n{} did a FABULOUS PERFORMANCE!", dolphin.name);
|
||||||
|
// for response in responses {
|
||||||
|
// println!(" {}", response);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,15 +9,15 @@ edition = "2021"
|
|||||||
# Challenge Help 1: If you choose to take on the challenge, you'll need to add `criterion` as a
|
# Challenge Help 1: If you choose to take on the challenge, you'll need to add `criterion` as a
|
||||||
# development dependency. Here is one way to do it:
|
# development dependency. Here is one way to do it:
|
||||||
|
|
||||||
# [dev-dependencies]
|
[dev-dependencies]
|
||||||
# criterion = { version = "0.3", features = ["html_reports"] }
|
criterion = { version = "0.3", features = ["html_reports"] }
|
||||||
|
|
||||||
# Challenge Help 2: Each benchmark needs a `[[bench]]` section with a name and disabling the harness.
|
# Challenge Help 2: Each benchmark needs a `[[bench]]` section with a name and disabling the harness.
|
||||||
# A name "somename" will correspond with a file "benches/somename.rs"
|
# A name "somename" will correspond with a file "benches/somename.rs"
|
||||||
|
|
||||||
# [[bench]]
|
[[bench]]
|
||||||
# name = "somename"
|
name = "somename"
|
||||||
# harness = false
|
harness = false
|
||||||
|
|
||||||
# Challenge Help 3: The Criterion documentation has a great tutorial for how to actually write your
|
# Challenge Help 3: The Criterion documentation has a great tutorial for how to actually write your
|
||||||
# benchmark. Don't skip the part about `black_box()`!
|
# benchmark. Don't skip the part about `black_box()`!
|
||||||
|
|||||||
9
exercise/testing/benches/somename.rs
Normal file
9
exercise/testing/benches/somename.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
||||||
|
use testing::sploosh;
|
||||||
|
|
||||||
|
pub fn sploosh_benchmark(c: &mut Criterion) {
|
||||||
|
c.bench_function("sploosh", |b| b.iter(|| sploosh(black_box(8), black_box(9), black_box(10))));
|
||||||
|
}
|
||||||
|
|
||||||
|
criterion_group!(benches, sploosh_benchmark);
|
||||||
|
criterion_main!(benches);
|
||||||
@@ -3,6 +3,7 @@ pub fn sploosh(x: i32, y: i32, z: i32) -> i32 {
|
|||||||
(x, _, _) if x < 0 => 99,
|
(x, _, _) if x < 0 => 99,
|
||||||
(1, 2, 3) => 4,
|
(1, 2, 3) => 4,
|
||||||
(5, 6, 7) => 3,
|
(5, 6, 7) => 3,
|
||||||
|
(8, 9, 10) => 7,
|
||||||
(x, y, z) => x + y - z,
|
(x, y, z) => x + y - z,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -13,9 +14,11 @@ pub fn splish(a: i32, b: i32) -> i32 {
|
|||||||
|
|
||||||
// 1. Use the `cfg` attribute to mark the `test` module below as a test module
|
// 1. Use the `cfg` attribute to mark the `test` module below as a test module
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
// 2. Bring all the library items into scope with a `use` statement
|
// 2. Bring all the library items into scope with a `use` statement
|
||||||
// Hint: It's okay to use `*` here.
|
// Hint: It's okay to use `*` here.
|
||||||
|
use super::*;
|
||||||
|
|
||||||
// 3. Write a test function that verifies the following condition using the `assert_eq!` or
|
// 3. Write a test function that verifies the following condition using the `assert_eq!` or
|
||||||
// `assert_ne!` macros
|
// `assert_ne!` macros
|
||||||
@@ -26,10 +29,24 @@ mod test {
|
|||||||
// `cargo test` should run your tests and pass
|
// `cargo test` should run your tests and pass
|
||||||
// Hint: Don't forget the `#[test]` attribute for your test function!
|
// Hint: Don't forget the `#[test]` attribute for your test function!
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sploosh() {
|
||||||
|
assert_eq!(sploosh(1, 2, 3), 4);
|
||||||
|
assert_ne!(sploosh(5, 6, 7), 4);
|
||||||
|
assert_eq!(sploosh(-1, 2, 3), 99);
|
||||||
|
}
|
||||||
|
|
||||||
// 4. Write a test function that verifies the following conditions using the `assert!` macro
|
// 4. Write a test function that verifies the following conditions using the `assert!` macro
|
||||||
// - splish(100, 10) is negative
|
// - splish(100, 10) is negative
|
||||||
// - splish(40, 20) is positive
|
// - splish(40, 20) is positive
|
||||||
// - splish(9, 3) is 0
|
// - splish(9, 3) is 0
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_splish() {
|
||||||
|
assert!(splish(100, 10) < 0);
|
||||||
|
assert!(splish(40, 20) > 0);
|
||||||
|
assert!(splish(9, 3) == 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Create a `tests/` directory and an integration test file `tests/more_tests.rs`
|
// 5. Create a `tests/` directory and an integration test file `tests/more_tests.rs`
|
||||||
|
|||||||
6
exercise/testing/tests/more_tests.rs
Normal file
6
exercise/testing/tests/more_tests.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
use testing::{sploosh, splish};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_sploosh_splish() {
|
||||||
|
assert_eq!(sploosh(splish(-1, 0), splish(1, 1), splish(3, 2)), 4);
|
||||||
|
}
|
||||||
@@ -1,15 +1,39 @@
|
|||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub enum Cake {
|
pub enum Cake {
|
||||||
Chocolate,
|
Chocolate,
|
||||||
MapleBacon,
|
MapleBacon,
|
||||||
Spice,
|
Spice,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Party {
|
pub struct Party {
|
||||||
pub at_restaurant: bool,
|
pub at_restaurant: bool,
|
||||||
pub num_people: u8,
|
pub num_people: u8,
|
||||||
pub cake: Cake,
|
pub cake: Cake,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Party {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
at_restaurant: true,
|
||||||
|
num_people: 8,
|
||||||
|
cake: Cake::Chocolate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Party {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.cake == other.cake
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&Party> for Cake {
|
||||||
|
fn from(party: &Party) -> Self {
|
||||||
|
party.cake
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// 1. The code below doesn't work because Cake doesn't implement Debug.
|
// 1. The code below doesn't work because Cake doesn't implement Debug.
|
||||||
// - Derive the Debug trait for the Cake enum above so this code will work. Then, run the code.
|
// - Derive the Debug trait for the Cake enum above so this code will work. Then, run the code.
|
||||||
@@ -23,11 +47,11 @@ fn main() {
|
|||||||
// function instead of moved.
|
// function instead of moved.
|
||||||
// - Hint: You may need to derive another trait in order to be able to derive the Copy trait
|
// - Hint: You may need to derive another trait in order to be able to derive the Copy trait
|
||||||
|
|
||||||
// match cake {
|
match cake {
|
||||||
// Cake::Chocolate => println!("The name's Chocolate. Dark...Chocolate."),
|
Cake::Chocolate => println!("The name's Chocolate. Dark...Chocolate."),
|
||||||
// Cake::MapleBacon => println!("Dreams do come true!"),
|
Cake::MapleBacon => println!("Dreams do come true!"),
|
||||||
// Cake::Spice => println!("Great, let's spice it up!"),
|
Cake::Spice => println!("Great, let's spice it up!"),
|
||||||
// }
|
}
|
||||||
|
|
||||||
// 3. Uncomment the println below. It doesn't work since the Party struct doesn't implement the
|
// 3. Uncomment the println below. It doesn't work since the Party struct doesn't implement the
|
||||||
// Debug or Default traits.
|
// Debug or Default traits.
|
||||||
@@ -44,7 +68,7 @@ fn main() {
|
|||||||
// Hint: If you get stuck, there is an example at
|
// Hint: If you get stuck, there is an example at
|
||||||
// https://doc.rust-lang.org/std/default/trait.Default.html#how-can-i-implement-default
|
// https://doc.rust-lang.org/std/default/trait.Default.html#how-can-i-implement-default
|
||||||
|
|
||||||
// println!("The default Party is\n{:#?}", Party::default());
|
println!("The default Party is\n{:#?}", Party::default());
|
||||||
|
|
||||||
// 4. You prefer Maple Bacon cake. Use "struct update syntax" to create a Party with `cake`
|
// 4. You prefer Maple Bacon cake. Use "struct update syntax" to create a Party with `cake`
|
||||||
// set to `Cake::MapleBacon`, but the rest of the values are default.
|
// set to `Cake::MapleBacon`, but the rest of the values are default.
|
||||||
@@ -52,10 +76,11 @@ fn main() {
|
|||||||
// Hint: The trick to struct update syntax is specifying the value(s) you want to customize
|
// Hint: The trick to struct update syntax is specifying the value(s) you want to customize
|
||||||
// first and then ending the struct with `..Default::default()` -- but no comma after that!
|
// first and then ending the struct with `..Default::default()` -- but no comma after that!
|
||||||
|
|
||||||
// let party = Party {
|
let party = Party {
|
||||||
// ...
|
cake: Cake::MapleBacon,
|
||||||
// };
|
..Default::default()
|
||||||
// println!("Yes! My party has my favorite {:?} cake!", party.cake);
|
};
|
||||||
|
println!("Yes! My party has my favorite {:?} cake!", party.cake);
|
||||||
|
|
||||||
// 5. Parties are "equal" if they have the same cake.
|
// 5. Parties are "equal" if they have the same cake.
|
||||||
// - Derive the PartialEq trait for the Cake enum so Cakes can be compared.
|
// - Derive the PartialEq trait for the Cake enum so Cakes can be compared.
|
||||||
@@ -63,14 +88,15 @@ fn main() {
|
|||||||
// then they are equal, no matter the location or number of attendees at the party.
|
// then they are equal, no matter the location or number of attendees at the party.
|
||||||
// - Uncomment and run the code below.
|
// - Uncomment and run the code below.
|
||||||
|
|
||||||
// let other_party = Party {
|
let other_party = Party {
|
||||||
// at_restaurant: false,
|
at_restaurant: false,
|
||||||
// num_people: 235,
|
num_people: 235,
|
||||||
// cake: Cake::MapleBacon,
|
cake: Cake::MapleBacon,
|
||||||
// };
|
};
|
||||||
// if party == other_party {
|
|
||||||
// println!("Your party is just like mine!");
|
if party == other_party {
|
||||||
// }
|
println!("Your party is just like mine!");
|
||||||
|
}
|
||||||
|
|
||||||
// Challenge: You would like to be able to pass a Party struct into the smell_cake() function
|
// Challenge: You would like to be able to pass a Party struct into the smell_cake() function
|
||||||
// which takes a type T which implements the Into<Cake> trait.
|
// which takes a type T which implements the Into<Cake> trait.
|
||||||
@@ -78,20 +104,19 @@ fn main() {
|
|||||||
// - Implement `From<Party> for Cake` so that the function call below works.
|
// - Implement `From<Party> for Cake` so that the function call below works.
|
||||||
//
|
//
|
||||||
|
|
||||||
// smell_cake(party);
|
smell_cake(&party);
|
||||||
|
|
||||||
// Challenge 2: Implement `From<&Party> for Cake` so that you can smell your cake without
|
// Challenge 2: Implement `From<&Party> for Cake` so that you can smell your cake without
|
||||||
// consuming it. Change the code above to pass in a &party. Then uncomment and run the code
|
// consuming it. Change the code above to pass in a &party. Then uncomment and run the code
|
||||||
// below. After all, you want to smell your cake and eat it, too!
|
// below. After all, you want to smell your cake and eat it, too!
|
||||||
|
|
||||||
// println!("Yum! I'm eating this cake: {:?}. Oops, I dropped it on the floor.", party.cake);
|
println!("Yum! I'm eating this cake: {:?}. Oops, I dropped it on the floor.", party.cake);
|
||||||
// drop(cake);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn admire_cake(cake: Cake) {
|
pub fn admire_cake(cake: Cake) {
|
||||||
println!("What a nice {:?} cake! 🎂", cake);
|
println!("What a nice {:?} cake! 🎂", cake);
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn smell_cake<T: Into<Cake>>(something: T) {
|
pub fn smell_cake<T: Into<Cake>>(something: T) {
|
||||||
// println!("Hmm...something smells like a {:?} cake!", something.into());
|
println!("Hmm...something smells like a {:?} cake!", something.into());
|
||||||
// }
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user