error_handling done
This commit is contained in:
parent
4f31cb6daf
commit
bfe2e1d8f0
|
@ -9,14 +9,12 @@
|
||||||
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
pub fn generate_nametag_text(name: String) -> Option<String> {
|
pub fn generate_nametag_text(name: String) -> Result<String, String> {
|
||||||
if name.is_empty() {
|
if name.is_empty() {
|
||||||
// Empty names aren't allowed.
|
Err(format!("`name` was empty; it must be nonempty."))
|
||||||
None
|
|
||||||
} else {
|
} else {
|
||||||
Some(format!("Hi! My name is {}", name))
|
Ok(format!("Hi! My name is {}", name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,14 +19,13 @@
|
||||||
// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
||||||
let processing_fee = 1;
|
let processing_fee = 1;
|
||||||
let cost_per_item = 5;
|
let cost_per_item = 5;
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
|
@ -15,7 +14,7 @@ fn main() {
|
||||||
let mut tokens = 100;
|
let mut tokens = 100;
|
||||||
let pretend_user_input = "8";
|
let pretend_user_input = "8";
|
||||||
|
|
||||||
let cost = total_cost(pretend_user_input)?;
|
let cost = total_cost(pretend_user_input).expect("Parse Int Error");
|
||||||
|
|
||||||
if cost > tokens {
|
if cost > tokens {
|
||||||
println!("You can't afford that many!");
|
println!("You can't afford that many!");
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
struct PositiveNonzeroInteger(u64);
|
struct PositiveNonzeroInteger(u64);
|
||||||
|
@ -16,8 +15,11 @@ enum CreationError {
|
||||||
|
|
||||||
impl PositiveNonzeroInteger {
|
impl PositiveNonzeroInteger {
|
||||||
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
||||||
// Hmm... Why is this always returning an Ok value?
|
match value.signum() {
|
||||||
Ok(PositiveNonzeroInteger(value as u64))
|
-1 => Err(CreationError::Negative),
|
||||||
|
0 => Err(CreationError::Zero),
|
||||||
|
_ => Ok(PositiveNonzeroInteger(value as u64))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,14 +22,13 @@
|
||||||
// Execute `rustlings hint errors5` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint errors5` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::error;
|
use std::error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
// TODO: update the return type of `main()` to make this compile.
|
// TODO: update the return type of `main()` to make this compile.
|
||||||
fn main() -> Result<(), Box<dyn ???>> {
|
fn main() -> Result<(), Box<dyn error::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)?);
|
||||||
|
|
|
@ -9,7 +9,6 @@
|
||||||
// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
|
@ -26,12 +25,16 @@ impl ParsePosNonzeroError {
|
||||||
}
|
}
|
||||||
// TODO: add another error conversion function here.
|
// TODO: add another error conversion function here.
|
||||||
// fn from_parseint...
|
// fn from_parseint...
|
||||||
|
fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError {
|
||||||
|
ParsePosNonzeroError::ParseInt(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_pos_nonzero(s: &str) -> Result<PositiveNonzeroInteger, ParsePosNonzeroError> {
|
fn parse_pos_nonzero(s: &str) -> Result<PositiveNonzeroInteger, 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().unwrap();
|
||||||
|
let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parseint)?;
|
||||||
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
|
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue