errors exercise: improve instructions, add a challenge

This commit is contained in:
Nathan Stocks
2021-08-18 21:01:50 -06:00
parent adc212fbbb
commit db5fbc33be
2 changed files with 17 additions and 15 deletions

View File

@@ -1,18 +1,18 @@
// 1. Create a DolphinError type representing the following three conditions:
// - Hungry - An dolphin is hungry
// - TooYoung - An dolphin is too young
// - Hungry - The dolphin is hungry
// - TooYoung - The dolphin is too young
// - LongName - The dolphin's name is too long and annoying to say
//
// As a reminder, here are the 5 Guidelines for creating an error type:
// (1) Use an `enum` for your error type
// (2) Your error conditions should be enum variants grouped in as few enums as makes sense
// (3) Don't expose error types other than your own (you don't have to do anything for this one)
// (3) Don't expose error types other than your own (not going to be a problem for this exercise)
// (4) Make your enum non-exhaustive
// (5) Implement the Debug, Display, and Error traits
// (5b) You can use thiserror's `Error` macro to derive Display and Error.h
// (5b) You can use thiserror's `Error` macro to derive the Display and Error traits
//
// Once you have completed the error type correctly, you should be able to run `cargo build --lib`
// without any errors.
// 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
// pub enum DolphinError...

View File

@@ -3,6 +3,7 @@
use aquarium::Dolphin;
// (You already did #1 in lib.rs, right?)
//
// 2a. Uncomment and finish the play_time function below
// - Bring anyhow::Result into scope with a `use` statement
// - Have the play_time function return a `Result<Vec<String>>`. The vector of Strings will
@@ -10,18 +11,16 @@ use aquarium::Dolphin;
// fn play_time(dolphin: &Dolphin) -> ... {
// let mut responses = vec![];
// // 2b. There are three methods on the Dolphin struct:
// // - .say_your_name()
// // - .flip()
// // - .shake_hands()
// // 2b. Call the .say_your_name() method on `dolphin`, use `?` to unwrap the value, and push
// // the value onto the `responses` vector.
// //
// // For each of the three methods above:
// // - Call the method on `dolphin`, using the `?` operator to unwrap the value / return the error
// // - Push the unwrapped string onto the `responses` vector using the .push() method
// // 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)
// }
@@ -49,6 +48,9 @@ fn main() {
},
];
for dolphin in &dolphins {
// Challenge: Change main() so that it returns a Result, and instead of handling the error
// that play_time returns, use the try operator to only handle the success condition. How
// does the output of the program change?
match play_time(dolphin) {
Ok(responses) => {
println!("{} did a FABULOUS PERFORMANCE!", dolphin.name);