errors exercise: improve instructions, add a challenge
This commit is contained in:
@@ -1,18 +1,18 @@
|
|||||||
// 1. Create a DolphinError type representing the following three conditions:
|
// 1. Create a DolphinError type representing the following three conditions:
|
||||||
// - Hungry - An dolphin is hungry
|
// - Hungry - The dolphin is hungry
|
||||||
// - TooYoung - An dolphin is too young
|
// - TooYoung - The dolphin is too young
|
||||||
// - LongName - The dolphin's name is too long and annoying to say
|
// - 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:
|
// As a reminder, here are the 5 Guidelines for creating an error type:
|
||||||
// (1) Use an `enum` for your 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
|
// (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
|
// (4) Make your enum non-exhaustive
|
||||||
// (5) Implement the Debug, Display, and Error traits
|
// (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`
|
// Once you have completed defining the error type correctly, you should be able to run
|
||||||
// without any errors.
|
// `cargo build --lib` without any build errors or warnings. Then go to main.rs and continue with #2
|
||||||
|
|
||||||
// pub enum DolphinError...
|
// pub enum DolphinError...
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
use aquarium::Dolphin;
|
use aquarium::Dolphin;
|
||||||
|
|
||||||
// (You already did #1 in lib.rs, right?)
|
// (You already did #1 in lib.rs, right?)
|
||||||
|
//
|
||||||
// 2a. Uncomment and finish the play_time function below
|
// 2a. Uncomment and finish the play_time function below
|
||||||
// - Bring anyhow::Result into scope with a `use` statement
|
// - Bring anyhow::Result into scope with a `use` statement
|
||||||
// - 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
|
||||||
@@ -10,18 +11,16 @@ use aquarium::Dolphin;
|
|||||||
|
|
||||||
// fn play_time(dolphin: &Dolphin) -> ... {
|
// fn play_time(dolphin: &Dolphin) -> ... {
|
||||||
// let mut responses = vec![];
|
// let mut responses = vec![];
|
||||||
// // 2b. There are three methods on the Dolphin struct:
|
// // 2b. Call the .say_your_name() method on `dolphin`, use `?` to unwrap the value, and push
|
||||||
// // - .say_your_name()
|
// // the value onto the `responses` vector.
|
||||||
// // - .flip()
|
|
||||||
// // - .shake_hands()
|
|
||||||
// //
|
// //
|
||||||
// // 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...
|
// // let response = ... // this can be done with an intermediate variable...
|
||||||
// // responses.push( ... ) // ...or all on one line. Either way is fine!
|
// // 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)
|
// Ok(responses)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
@@ -49,6 +48,9 @@ fn main() {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
for dolphin in &dolphins {
|
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) {
|
match play_time(dolphin) {
|
||||||
Ok(responses) => {
|
Ok(responses) => {
|
||||||
println!("{} did a FABULOUS PERFORMANCE!", dolphin.name);
|
println!("{} did a FABULOUS PERFORMANCE!", dolphin.name);
|
||||||
|
|||||||
Reference in New Issue
Block a user