Switch from Orca to Dolphin. Finish up removing code to be implemented.
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
// 1. Create error type(s) representing the following three conditions:
|
// 1. Create a DolphinError type representing the following three conditions:
|
||||||
// - An orca is hungry (Hungry)
|
// - Hungry - An dolphin is hungry
|
||||||
// - An orca is too young (TooYoung)
|
// - TooYoung - An dolphin is too young
|
||||||
// - The orca's name is too long (LongName)
|
// - LongName - The dolphin's name is too long and annoying to say
|
||||||
//
|
//
|
||||||
// As a reminder, here are the 5 Guidelines for creating an error type
|
// As a reminder, here are the 5 Guidelines for creating an error type
|
||||||
// (1) Use an `enum` for your 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
|
// (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
|
// (3) Don't expose error types other than your own (you don't have to do anything for this one)
|
||||||
// (4) Make your enum non-exhaustive
|
// (4) Make your enum non-exhaustive
|
||||||
// (5) Implement the Debug, Display, and Error traits
|
// (5) Implement the Debug, Display, and Error traits
|
||||||
// (5b) You can use thiserror's `Error` macro to derive Display and Error.h
|
// (5b) You can use thiserror's `Error` macro to derive Display and Error.h
|
||||||
@@ -14,45 +14,32 @@
|
|||||||
// Once you have completed the error type correctly, you should be able to run `cargo build --lib`
|
// Once you have completed the error type correctly, you should be able to run `cargo build --lib`
|
||||||
// without any errors.
|
// without any errors.
|
||||||
|
|
||||||
// pub enum OrcaError...
|
// pub enum DolphinError...
|
||||||
|
|
||||||
use thiserror::Error;
|
pub struct Dolphin {
|
||||||
|
|
||||||
#[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 name: String,
|
||||||
pub age: u8,
|
pub age: u8,
|
||||||
pub hungry: bool,
|
pub hungry: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Orca {
|
impl Dolphin {
|
||||||
pub fn say_your_name(&self) -> Result<String, OrcaError> {
|
pub fn say_your_name(&self) -> Result<String, DolphinError> {
|
||||||
if self.name.len() > 10 {
|
if self.name.len() > 10 {
|
||||||
Err(OrcaError::LongName)
|
Err(DolphinError::LongName)
|
||||||
} else {
|
} else {
|
||||||
Ok(format!("Hi, my name is {} and I'm an Orca!", self.name))
|
Ok(format!("Hi, my name is {} and I'm a Dolphin!", self.name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn flip(&self) -> Result<String, OrcaError> {
|
pub fn flip(&self) -> Result<String, DolphinError> {
|
||||||
if self.age < 4 {
|
if self.age < 4 {
|
||||||
Err(OrcaError::TooYoung)
|
Err(DolphinError::TooYoung)
|
||||||
} else {
|
} else {
|
||||||
Ok(format!("Yippee, I'm doing a flip!"))
|
Ok(format!("Yippee, I'm doing a flip!"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn shake_hands(&self) -> Result<String, OrcaError> {
|
pub fn shake_hands(&self) -> Result<String, DolphinError> {
|
||||||
if self.hungry {
|
if self.hungry {
|
||||||
Err(OrcaError::Hungry)
|
Err(DolphinError::Hungry)
|
||||||
} else {
|
} else {
|
||||||
Ok(format!("Nice to meet you, let's shake hands!"))
|
Ok(format!("Nice to meet you, let's shake hands!"))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,22 @@
|
|||||||
// START IN lib.rs!
|
// START IN lib.rs!
|
||||||
|
|
||||||
use aquarium::Orca;
|
use aquarium::Dolphin;
|
||||||
|
|
||||||
// (You already did #1 in lib.rs, right?)
|
// (You already did #1 in lib.rs, right?)
|
||||||
// 2a. Uncomment and finish the play_time function below
|
// 2a. Uncomment and finish the play_time function below
|
||||||
// - Bring anyhow::Result into scope with a `use` statement
|
// - Bring anyhow::Result into scope with a `use` statement
|
||||||
// - Have the function return a Result<Vec<String>>. The vector of Strings will represent successful
|
// - Have the play_time function return a `Result<Vec<String>>`. The vector of Strings will
|
||||||
// outcomes of various performances.
|
// represent successful outcomes of various dolphin tricks.
|
||||||
|
|
||||||
// fn play_time(orca: &Orca) -> ... {
|
// fn play_time(dolphin: &Dolphin) -> ... {
|
||||||
// let mut responses = vec![];
|
// let mut responses = vec![];
|
||||||
// // 2b. There are three methods on the Orca struct:
|
// // 2b. There are three methods on the Dolphin struct:
|
||||||
// // - .say_your_name()
|
// // - .say_your_name()
|
||||||
// // - .flip()
|
// // - .flip()
|
||||||
// // - .shake_hands()
|
// // - .shake_hands()
|
||||||
// //
|
// //
|
||||||
// // For each of the three methods above:
|
// // For each of the three methods above:
|
||||||
// // - Call the method on `orca`, using the `?` operator to unwrap the value / return the error
|
// // - 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
|
// // - Push the unwrapped string onto the `responses` vector using the .push() method
|
||||||
|
|
||||||
// // let response = ... // this can be done with an intermediate variable...
|
// // let response = ... // this can be done with an intermediate variable...
|
||||||
@@ -26,37 +26,37 @@ use aquarium::Orca;
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let orcas = vec![
|
let dolphins = vec![
|
||||||
Orca {
|
Dolphin {
|
||||||
name: "Augustinius".into(),
|
name: "Augustinius".into(),
|
||||||
age: 7,
|
age: 7,
|
||||||
hungry: false,
|
hungry: false,
|
||||||
},
|
},
|
||||||
Orca {
|
Dolphin {
|
||||||
name: "Bitty".into(),
|
name: "Bitty".into(),
|
||||||
age: 2,
|
age: 2,
|
||||||
hungry: true,
|
hungry: true,
|
||||||
},
|
},
|
||||||
Orca {
|
Dolphin {
|
||||||
name: "Carson".into(),
|
name: "Carson".into(),
|
||||||
age: 5,
|
age: 5,
|
||||||
hungry: true,
|
hungry: true,
|
||||||
},
|
},
|
||||||
Orca {
|
Dolphin {
|
||||||
name: "Devin".into(),
|
name: "Devin".into(),
|
||||||
age: 6,
|
age: 6,
|
||||||
hungry: false,
|
hungry: false,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
for orca in &orcas {
|
for dolphin in &dolphins {
|
||||||
match play_time(orca) {
|
match play_time(dolphin) {
|
||||||
Ok(responses) => {
|
Ok(responses) => {
|
||||||
println!("{} did a FABULOUS PERFORMANCE!", orca.name);
|
println!("{} 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: {}", orca.name, e.to_string()),
|
Err(e) => println!("{} can't perform today: {}", dolphin.name, e.to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user