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

@@ -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);