From 8253dc90b14e30876c0a52c1a2aed8720fb7540b Mon Sep 17 00:00:00 2001 From: Pedro Morais Date: Tue, 20 Feb 2024 11:31:51 +0000 Subject: [PATCH 1/2] error exercises --- exercises/13_error_handling/errors1.rs | 10 ++++------ exercises/13_error_handling/errors2.rs | 4 ++-- exercises/13_error_handling/errors3.rs | 4 ++-- exercises/13_error_handling/errors4.rs | 11 ++++++++--- exercises/13_error_handling/errors5.rs | 4 +--- exercises/13_error_handling/errors6.rs | 10 +++++----- 6 files changed, 22 insertions(+), 21 deletions(-) diff --git a/exercises/13_error_handling/errors1.rs b/exercises/13_error_handling/errors1.rs index 0ba59a5..6a803a0 100644 --- a/exercises/13_error_handling/errors1.rs +++ b/exercises/13_error_handling/errors1.rs @@ -9,14 +9,12 @@ // 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 { +pub fn generate_nametag_text(name: String) ->Result { 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)) } } diff --git a/exercises/13_error_handling/errors2.rs b/exercises/13_error_handling/errors2.rs index 631fe67..f9b2628 100644 --- a/exercises/13_error_handling/errors2.rs +++ b/exercises/13_error_handling/errors2.rs @@ -19,14 +19,14 @@ // 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 { let processing_fee = 1; let cost_per_item = 5; - let qty = item_quantity.parse::(); + let qty = item_quantity.parse::()?; Ok(qty * cost_per_item + processing_fee) } diff --git a/exercises/13_error_handling/errors3.rs b/exercises/13_error_handling/errors3.rs index d42d3b1..bca37d8 100644 --- a/exercises/13_error_handling/errors3.rs +++ b/exercises/13_error_handling/errors3.rs @@ -7,11 +7,10 @@ // Execute `rustlings hint errors3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE use std::num::ParseIntError; -fn main() { +fn main() -> Result<(), ParseIntError> { let mut tokens = 100; let pretend_user_input = "8"; @@ -23,6 +22,7 @@ fn main() { tokens -= cost; println!("You now have {} tokens.", tokens); } + Ok(()) } pub fn total_cost(item_quantity: &str) -> Result { diff --git a/exercises/13_error_handling/errors4.rs b/exercises/13_error_handling/errors4.rs index d6d6fcb..f2f3b2a 100644 --- a/exercises/13_error_handling/errors4.rs +++ b/exercises/13_error_handling/errors4.rs @@ -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); @@ -17,7 +15,14 @@ enum CreationError { impl PositiveNonzeroInteger { fn new(value: i64) -> Result { // 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)) + } + } } diff --git a/exercises/13_error_handling/errors5.rs b/exercises/13_error_handling/errors5.rs index 92461a7..722c778 100644 --- a/exercises/13_error_handling/errors5.rs +++ b/exercises/13_error_handling/errors5.rs @@ -22,14 +22,12 @@ // Execute `rustlings hint errors5` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - use std::error; use std::fmt; use std::num::ParseIntError; // TODO: update the return type of `main()` to make this compile. -fn main() -> Result<(), Box> { +fn main() -> Result<(), Box> { let pretend_user_input = "42"; let x: i64 = pretend_user_input.parse()?; println!("output={:?}", PositiveNonzeroInteger::new(x)?); diff --git a/exercises/13_error_handling/errors6.rs b/exercises/13_error_handling/errors6.rs index aaf0948..1b6d403 100644 --- a/exercises/13_error_handling/errors6.rs +++ b/exercises/13_error_handling/errors6.rs @@ -9,8 +9,6 @@ // Execute `rustlings hint errors6` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - use std::num::ParseIntError; // This is a custom error type that we will be using in `parse_pos_nonzero()`. @@ -25,14 +23,16 @@ 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 { // TODO: change this to return an appropriate error instead of panicking // when `parse()` returns an error. - let x: i64 = s.parse().unwrap(); - PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation) + let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parseint)?; + PositiveNonzeroInteger::new(x).map_err((ParsePosNonzeroError::from_creation)) } // Don't change anything below this line. From 9cef91a1f68bc458e0fc683c56c830104950142d Mon Sep 17 00:00:00 2001 From: Pedro Morais Date: Tue, 20 Feb 2024 11:34:50 +0000 Subject: [PATCH 2/2] error exercises --- exercises/13_error_handling/errors6.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/13_error_handling/errors6.rs b/exercises/13_error_handling/errors6.rs index 1b6d403..a1b9375 100644 --- a/exercises/13_error_handling/errors6.rs +++ b/exercises/13_error_handling/errors6.rs @@ -21,7 +21,7 @@ enum ParsePosNonzeroError { impl ParsePosNonzeroError { fn from_creation(err: CreationError) -> ParsePosNonzeroError { ParsePosNonzeroError::Creation(err) - } + } // TODO: add another error conversion function here. fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError { ParsePosNonzeroError::ParseInt((err))