Complete exercise: errors
This commit is contained in:
@@ -14,7 +14,18 @@
|
||||
// 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...
|
||||
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 name: String,
|
||||
|
||||
@@ -11,22 +11,26 @@ use aquarium::Dolphin;
|
||||
// - Have the play_time function return a `Result<Vec<String>>`. The vector of Strings will
|
||||
// represent successful outcomes of various dolphin tricks.
|
||||
|
||||
// fn play_time(dolphin: &Dolphin) -> ... {
|
||||
// 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)
|
||||
// }
|
||||
use anyhow::Result;
|
||||
|
||||
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![
|
||||
Dolphin {
|
||||
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
|
||||
// main(), which will end the program at the first error. anyhow's Result will take care of
|
||||
// formatting the error output for us.
|
||||
|
||||
match play_time(dolphin) {
|
||||
Ok(responses) => {
|
||||
println!("{} did a FABULOUS PERFORMANCE!", dolphin.name);
|
||||
println!("\n{} did a FABULOUS PERFORMANCE!", dolphin.name);
|
||||
for response in responses {
|
||||
println!(" {}", response);
|
||||
}
|
||||
}
|
||||
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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user