Add errors exercise
This commit is contained in:
@@ -8,5 +8,6 @@ members = [
|
|||||||
"exercise/idiomatic",
|
"exercise/idiomatic",
|
||||||
"exercise/docs",
|
"exercise/docs",
|
||||||
"exercise/closures_iterators",
|
"exercise/closures_iterators",
|
||||||
"exercise/traits"
|
"exercise/traits",
|
||||||
|
"exercise/errors",
|
||||||
]
|
]
|
||||||
|
|||||||
11
exercise/errors/Cargo.toml
Normal file
11
exercise/errors/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
[package]
|
||||||
|
name = "aquarium"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Nathan Stocks <nathan@agileperception.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
# I thought you might want this for your library...
|
||||||
|
thiserror = "1.0"
|
||||||
|
# ...and this for your binary
|
||||||
|
anyhow = "1.0"
|
||||||
60
exercise/errors/src/lib.rs
Normal file
60
exercise/errors/src/lib.rs
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
// 1. Create error type(s) representing the following three conditions:
|
||||||
|
// - An orca is hungry (Hungry)
|
||||||
|
// - An orca is too young (TooYoung)
|
||||||
|
// - The orca's name is too long (LongName)
|
||||||
|
//
|
||||||
|
// As a reminder, here are the 5 Guidelines for creating an 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
|
||||||
|
// (3) Don't expose error types other than your own to users
|
||||||
|
// (4) Make your enum non-exhaustive
|
||||||
|
// (5) Implement the Debug, Display, and Error traits
|
||||||
|
// (5b) You can use thiserror's `Error` macro to derive Display and Error.h
|
||||||
|
//
|
||||||
|
// Once you have completed the error type correctly, you should be able to run `cargo build --lib`
|
||||||
|
// without any errors.
|
||||||
|
|
||||||
|
// pub enum OrcaError...
|
||||||
|
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
#[non_exhaustive]
|
||||||
|
pub enum OrcaError {
|
||||||
|
#[error("I'm too hungry to do that.")]
|
||||||
|
Hungry,
|
||||||
|
#[error("I'm too young to do that.")]
|
||||||
|
TooYoung,
|
||||||
|
#[error("I would, but my name is just so long!")]
|
||||||
|
LongName,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Orca {
|
||||||
|
pub name: String,
|
||||||
|
pub age: u8,
|
||||||
|
pub hungry: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Orca {
|
||||||
|
pub fn say_your_name(&self) -> Result<String, OrcaError> {
|
||||||
|
if self.name.len() > 10 {
|
||||||
|
Err(OrcaError::LongName)
|
||||||
|
} else {
|
||||||
|
Ok(format!("Hi, my name is {} and I'm an Orca!", self.name))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn flip(&self) -> Result<String, OrcaError> {
|
||||||
|
if self.age < 4 {
|
||||||
|
Err(OrcaError::TooYoung)
|
||||||
|
} else {
|
||||||
|
Ok(format!("Yippee, I'm doing a flip!"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn shake_hands(&self) -> Result<String, OrcaError> {
|
||||||
|
if self.hungry {
|
||||||
|
Err(OrcaError::Hungry)
|
||||||
|
} else {
|
||||||
|
Ok(format!("Nice to meet you, let's shake hands!"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
62
exercise/errors/src/main.rs
Normal file
62
exercise/errors/src/main.rs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
// START IN lib.rs!
|
||||||
|
|
||||||
|
use aquarium::Orca;
|
||||||
|
|
||||||
|
// (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 function return a Result<Vec<String>>. The vector of Strings will represent successful
|
||||||
|
// outcomes of various performances.
|
||||||
|
|
||||||
|
// fn play_time(orca: &Orca) -> ... {
|
||||||
|
// let mut responses = vec![];
|
||||||
|
// // 2b. There are three methods on the Orca struct:
|
||||||
|
// // - .say_your_name()
|
||||||
|
// // - .flip()
|
||||||
|
// // - .shake_hands()
|
||||||
|
// //
|
||||||
|
// // For each of the three methods above:
|
||||||
|
// // - Call the method on `orca`, 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!
|
||||||
|
|
||||||
|
// Ok(responses)
|
||||||
|
// }
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let orcas = vec![
|
||||||
|
Orca {
|
||||||
|
name: "Augustinius".into(),
|
||||||
|
age: 7,
|
||||||
|
hungry: false,
|
||||||
|
},
|
||||||
|
Orca {
|
||||||
|
name: "Bitty".into(),
|
||||||
|
age: 2,
|
||||||
|
hungry: true,
|
||||||
|
},
|
||||||
|
Orca {
|
||||||
|
name: "Carson".into(),
|
||||||
|
age: 5,
|
||||||
|
hungry: true,
|
||||||
|
},
|
||||||
|
Orca {
|
||||||
|
name: "Devin".into(),
|
||||||
|
age: 6,
|
||||||
|
hungry: false,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
for orca in &orcas {
|
||||||
|
match play_time(orca) {
|
||||||
|
Ok(responses) => {
|
||||||
|
println!("{} did a FABULOUS PERFORMANCE!", orca.name);
|
||||||
|
for response in responses {
|
||||||
|
println!(" {}", response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => println!("{} can't perform today: {}", orca.name, e.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user