mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 13:19:18 +00:00
Complete errors exercises
This commit is contained in:
parent
9159135bdb
commit
059c1bc872
@ -5,14 +5,12 @@
|
|||||||
// construct to `Option` that can be used to express error conditions. Let's use it!
|
// construct to `Option` that can be used to express error conditions. Let's use it!
|
||||||
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
pub fn generate_nametag_text(name: String) -> Result<String, String> {
|
||||||
|
|
||||||
pub fn generate_nametag_text(name: String) -> Option<String> {
|
|
||||||
if name.is_empty() {
|
if name.is_empty() {
|
||||||
// Empty names aren't allowed.
|
// Empty names aren't allowed.
|
||||||
None
|
Err("`name` was empty; it must be nonempty.".into())
|
||||||
} else {
|
} else {
|
||||||
Some(format!("Hi! My name is {}", name))
|
Ok(format!("Hi! My name is {}", name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -17,15 +17,12 @@
|
|||||||
// one is a lot shorter!
|
// one is a lot shorter!
|
||||||
// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,11 +4,9 @@
|
|||||||
// Why not? What should we do to fix it?
|
// Why not? What should we do to fix it?
|
||||||
// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
fn main() {
|
fn main() -> Result<(), ParseIntError> {
|
||||||
let mut tokens = 100;
|
let mut tokens = 100;
|
||||||
let pretend_user_input = "8";
|
let pretend_user_input = "8";
|
||||||
|
|
||||||
@ -16,9 +14,11 @@ 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,8 +1,6 @@
|
|||||||
// errors4.rs
|
// errors4.rs
|
||||||
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
struct PositiveNonzeroInteger(u64);
|
struct PositiveNonzeroInteger(u64);
|
||||||
|
|
||||||
@ -14,8 +12,13 @@ enum CreationError {
|
|||||||
|
|
||||||
impl PositiveNonzeroInteger {
|
impl PositiveNonzeroInteger {
|
||||||
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
||||||
// Hmm...? Why is this only returning an Ok value?
|
if value > 0 {
|
||||||
Ok(PositiveNonzeroInteger(value as u64))
|
Ok(PositiveNonzeroInteger(value as u64))
|
||||||
|
} else if value < 0 {
|
||||||
|
Err(CreationError::Negative)
|
||||||
|
} else {
|
||||||
|
Err(CreationError::Zero)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -16,14 +16,11 @@
|
|||||||
|
|
||||||
// Execute `rustlings hint errors5` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint errors5` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
use std::error::Error;
|
||||||
|
use std::fmt::Result as FmtResult;
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
|
|
||||||
use std::error;
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
use std::fmt;
|
|
||||||
use std::num::ParseIntError;
|
|
||||||
|
|
||||||
// TODO: update the return type of `main()` to make this compile.
|
|
||||||
fn main() -> Result<(), Box<dyn ???>> {
|
|
||||||
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)?);
|
||||||
@ -52,8 +49,8 @@ impl PositiveNonzeroInteger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// This is required so that `CreationError` can implement `error::Error`.
|
// This is required so that `CreationError` can implement `error::Error`.
|
||||||
impl fmt::Display for CreationError {
|
impl Display for CreationError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter) -> FmtResult {
|
||||||
let description = match *self {
|
let description = match *self {
|
||||||
CreationError::Negative => "number is negative",
|
CreationError::Negative => "number is negative",
|
||||||
CreationError::Zero => "number is zero",
|
CreationError::Zero => "number is zero",
|
||||||
@ -62,4 +59,4 @@ impl fmt::Display for CreationError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl error::Error for CreationError {}
|
impl Error for CreationError {}
|
||||||
|
|||||||
@ -8,8 +8,6 @@
|
|||||||
|
|
||||||
// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
// This is a custom error type that we will be using in `parse_pos_nonzero()`.
|
// This is a custom error type that we will be using in `parse_pos_nonzero()`.
|
||||||
@ -23,15 +21,17 @@ impl ParsePosNonzeroError {
|
|||||||
fn from_creation(err: CreationError) -> ParsePosNonzeroError {
|
fn from_creation(err: CreationError) -> ParsePosNonzeroError {
|
||||||
ParsePosNonzeroError::Creation(err)
|
ParsePosNonzeroError::Creation(err)
|
||||||
}
|
}
|
||||||
// TODO: add another error conversion function here.
|
|
||||||
// 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
|
match s.parse() {
|
||||||
// when `parse()` returns an error.
|
Ok(v) => PositiveNonzeroInteger::new(v).map_err(ParsePosNonzeroError::from_creation),
|
||||||
let x: i64 = s.parse().unwrap();
|
Err(parse_int_error) => Err(ParsePosNonzeroError::from_parseint(parse_int_error)),
|
||||||
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't change anything below this line.
|
// Don't change anything below this line.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user