From b25fe11bc7a8342053317cfc90d36930c574b55e Mon Sep 17 00:00:00 2001 From: Kishan Takoordyal Date: Mon, 25 May 2026 15:32:14 +0400 Subject: [PATCH] Complete exercise: errors --- exercise/errors/src/lib.rs | 13 ++++++++++- exercise/errors/src/main.rs | 45 ++++++++++++++++++++++++------------- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/exercise/errors/src/lib.rs b/exercise/errors/src/lib.rs index f631222..50ef2bf 100644 --- a/exercise/errors/src/lib.rs +++ b/exercise/errors/src/lib.rs @@ -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, diff --git a/exercise/errors/src/main.rs b/exercise/errors/src/main.rs index eea299f..4399d05 100644 --- a/exercise/errors/src/main.rs +++ b/exercise/errors/src/main.rs @@ -11,22 +11,26 @@ use aquarium::Dolphin; // - Have the play_time function return a `Result>`. 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> { + 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(()) }