Skip to content

Commit

Permalink
errors solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Matrx123 committed Apr 12, 2024
1 parent 65c5a64 commit 0ef1a42
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
9 changes: 3 additions & 6 deletions exercises/13_error_handling/errors1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a
// 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() {
// Empty names aren't allowed.
None
Err(format!("`name` was empty; it must be nonempty."))
} else {
Some(format!("Hi! My name is {}", name))
Ok(format!("Hi! My name is {}", name))
}
}

Expand Down
11 changes: 8 additions & 3 deletions exercises/13_error_handling/errors2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@
// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::num::ParseIntError;

pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
let processing_fee = 1;
let cost_per_item = 5;
let qty = item_quantity.parse::<i32>();

//1st way
let qty: i32 = match item_quantity.parse::<i32>() {
Ok(num) => num,
Err(e) => return Err(e),
};
//2nd way
//shorter and clear, basically does the same thing as 1st Way. i.e propogate errors
let qty: i32 = item_quantity.parse::<i32>()?;
Ok(qty * cost_per_item + processing_fee)
}

Expand Down
5 changes: 4 additions & 1 deletion exercises/13_error_handling/errors3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ fn main() {
let mut tokens = 100;
let pretend_user_input = "8";

let cost = total_cost(pretend_user_input)?;
let cost = match total_cost(pretend_user_input) {
Ok(val) => val,
Err(e) => panic!("There is an error"),
};

if cost > tokens {
println!("You can't afford that many!");
Expand Down
10 changes: 7 additions & 3 deletions exercises/13_error_handling/errors4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

#[derive(PartialEq, Debug)]
struct PositiveNonzeroInteger(u64);

Expand All @@ -17,7 +15,13 @@ enum CreationError {
impl PositiveNonzeroInteger {
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
// Hmm... Why is this always returning an Ok value?
Ok(PositiveNonzeroInteger(value as u64))
if value < 0 {
Err(CreationError::Negative)
} else if value == 0 {
Err(CreationError::Zero)
} else {
Ok(PositiveNonzeroInteger(value as u64))
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions exercises/13_error_handling/errors5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
// Execute `rustlings hint errors5` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::error;
use std::error::Error;
use std::fmt;
use std::num::ParseIntError;

// TODO: update the return type of `main()` to make this compile.
fn main() -> Result<(), Box<dyn ???>> {
fn main() -> Result<(), Box<dyn Error>> {
let pretend_user_input = "42";
let x: i64 = pretend_user_input.parse()?;
println!("output={:?}", PositiveNonzeroInteger::new(x)?);
Expand Down
6 changes: 4 additions & 2 deletions exercises/13_error_handling/errors6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ impl ParsePosNonzeroError {
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> {
// TODO: change this to return an appropriate error instead of panicking
// when `parse()` returns an error.
let x: i64 = s.parse().unwrap();
let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parseint)?;
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
}

Expand Down

0 comments on commit 0ef1a42

Please sign in to comment.