Complete exercise: errors

This commit is contained in:
2026-05-25 15:32:14 +04:00
parent 89fd568b75
commit b25fe11bc7
2 changed files with 41 additions and 17 deletions

View File

@@ -14,7 +14,18 @@
// Once you have completed defining the error type correctly, you should be able to run // 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 // `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 struct Dolphin {
pub name: String, pub name: String,

View File

@@ -11,22 +11,26 @@ use aquarium::Dolphin;
// - 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
// represent successful outcomes of various dolphin tricks. // represent successful outcomes of various dolphin tricks.
// fn play_time(dolphin: &Dolphin) -> ... { use anyhow::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 = ... // 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)
// }
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![ let dolphins = vec![
Dolphin { Dolphin {
name: "Augustinius".into(), 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 // 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 // main(), which will end the program at the first error. anyhow's Result will take care of
// formatting the error output for us. // formatting the error output for us.
match play_time(dolphin) { match play_time(dolphin) {
Ok(responses) => { Ok(responses) => {
println!("{} did a FABULOUS PERFORMANCE!", dolphin.name); println!("\n{} did a FABULOUS PERFORMANCE!", dolphin.name);
for response in responses { for response in responses {
println!(" {}", response); println!(" {}", response);
} }
} }
Err(e) => println!("{} can't perform today: {}", dolphin.name, e.to_string()), 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(())
} }