mirror of
https://git.aramjonghu.dev/AramJonghu/rustlings.git
synced 2026-09-07 10:13:31 +02:00
feat(errors): completed errors
This commit is contained in:
@@ -4,12 +4,12 @@
|
|||||||
// construct to `Option` that can be used to express error conditions. Change
|
// construct to `Option` that can be used to express error conditions. Change
|
||||||
// the function signature and body to return `Result<String, String>` instead
|
// the function signature and body to return `Result<String, String>` instead
|
||||||
// of `Option<String>`.
|
// of `Option<String>`.
|
||||||
fn generate_nametag_text(name: String) -> Option<String> {
|
fn generate_nametag_text(name: String) -> Result<String, String> {
|
||||||
if name.is_empty() {
|
if name.is_empty() {
|
||||||
// Empty names aren't allowed
|
// Empty names aren't allowed
|
||||||
None
|
Err("Empty names aren't allowed".to_string())
|
||||||
} else {
|
} else {
|
||||||
Some(format!("Hi! My name is {name}"))
|
Ok(format!("Hi! My name is {name}"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
|||||||
let cost_per_item = 5;
|
let cost_per_item = 5;
|
||||||
|
|
||||||
// TODO: Handle the error case as described above.
|
// TODO: Handle the error case as described above.
|
||||||
let qty = item_quantity.parse::<i32>();
|
let qty = item_quantity.parse::<i32>()?;
|
||||||
|
|
||||||
Ok(qty * cost_per_item + processing_fee)
|
Ok(qty * cost_per_item + processing_fee)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
|||||||
|
|
||||||
// TODO: Fix the compiler error by changing the signature and body of the
|
// TODO: Fix the compiler error by changing the signature and body of the
|
||||||
// `main` function.
|
// `main` function.
|
||||||
fn main() {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let mut tokens = 100;
|
let mut tokens = 100;
|
||||||
let pretend_user_input = "8";
|
let pretend_user_input = "8";
|
||||||
|
|
||||||
@@ -24,8 +24,10 @@ fn main() {
|
|||||||
|
|
||||||
if cost > tokens {
|
if cost > tokens {
|
||||||
println!("You can't afford that many!");
|
println!("You can't afford that many!");
|
||||||
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
tokens -= cost;
|
tokens -= cost;
|
||||||
println!("You now have {tokens} tokens.");
|
println!("You now have {tokens} tokens.");
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
enum CreationError {
|
enum CreationError {
|
||||||
Negative,
|
Negative,
|
||||||
@@ -11,7 +13,11 @@ impl PositiveNonzeroInteger {
|
|||||||
fn new(value: i64) -> Result<Self, CreationError> {
|
fn new(value: i64) -> Result<Self, CreationError> {
|
||||||
// TODO: This function shouldn't always return an `Ok`.
|
// TODO: This function shouldn't always return an `Ok`.
|
||||||
// Read the tests below to clarify what should be returned.
|
// Read the tests below to clarify what should be returned.
|
||||||
Ok(Self(value as u64))
|
match value.cmp(&0) {
|
||||||
|
Ordering::Less => Err(CreationError::Negative),
|
||||||
|
Ordering::Equal => Err(CreationError::Zero),
|
||||||
|
Ordering::Greater => Ok(Self(value as u64)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ impl PositiveNonzeroInteger {
|
|||||||
|
|
||||||
// TODO: Add the correct return type `Result<(), Box<dyn ???>>`. What can we
|
// TODO: Add the correct return type `Result<(), Box<dyn ???>>`. What can we
|
||||||
// use to describe both errors? Is there a trait which both errors implement?
|
// use to describe both errors? Is there a trait which both errors implement?
|
||||||
fn main() {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let pretend_user_input = "42";
|
let pretend_user_input = "42";
|
||||||
let x: i64 = pretend_user_input.parse()?;
|
let x: i64 = pretend_user_input.parse()?;
|
||||||
println!("output={:?}", PositiveNonzeroInteger::new(x)?);
|
println!("output={:?}", PositiveNonzeroInteger::new(x)?);
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ impl PositiveNonzeroInteger {
|
|||||||
fn parse(s: &str) -> Result<Self, ParsePosNonzeroError> {
|
fn parse(s: &str) -> Result<Self, ParsePosNonzeroError> {
|
||||||
// TODO: change this to return an appropriate error instead of panicking
|
// TODO: change this to return an appropriate error instead of panicking
|
||||||
// when `parse()` returns an error.
|
// when `parse()` returns an error.
|
||||||
let x: i64 = s.parse().unwrap();
|
let x: i64 = s.parse().map_err(ParsePosNonzeroError::ParseInt)?;
|
||||||
Self::new(x).map_err(ParsePosNonzeroError::Creation)
|
Self::new(x).map_err(ParsePosNonzeroError::Creation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user