From 0fe225974d588080fd4e4d25ac350965e5a2a027 Mon Sep 17 00:00:00 2001 From: Esteban Escobar Date: Sun, 22 Jan 2023 15:58:11 -0500 Subject: [PATCH] more progress --- REVISITS.md | 2 ++ exercises/error_handling/errors3.rs | 6 +++--- exercises/error_handling/errors4.rs | 11 ++++++++--- exercises/error_handling/errors5.rs | 4 +--- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/REVISITS.md b/REVISITS.md index a50ce4af..479f7444 100644 --- a/REVISITS.md +++ b/REVISITS.md @@ -16,4 +16,6 @@ return results to new Vec vector 15) options1 (understand Some and None) 16) option2 (important to understand if let, while let statements alternative to match ). ChatGPT explanation was great. 17) errors2 make sure you understand error propagation but also the long-form solution (just for understanding). "The ? placed after a Result value is defined to work in almost the same way as the match expressions we defined to handle the Result values in Listing 9-6. If the value of the Result is an Ok, the value inside the Ok will get returned from this expression, and the program will continue. If the value is an Err, the Err will be returned from the whole function as if we had used the return keyword so the error value gets propagated to the calling code." +18) errors3 +19) HAVE to revisit error5 - > great pre-intro to Box diff --git a/exercises/error_handling/errors3.rs b/exercises/error_handling/errors3.rs index a2d2d190..2cc8c623 100644 --- a/exercises/error_handling/errors3.rs +++ b/exercises/error_handling/errors3.rs @@ -4,11 +4,9 @@ // Why not? What should we do to fix it? // 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"; @@ -16,9 +14,11 @@ fn main() { if cost > tokens { println!("You can't afford that many!"); + Ok(()) } else { tokens -= cost; println!("You now have {} tokens.", tokens); + Ok(()) } } diff --git a/exercises/error_handling/errors4.rs b/exercises/error_handling/errors4.rs index 0efe8ccd..9d08087a 100644 --- a/exercises/error_handling/errors4.rs +++ b/exercises/error_handling/errors4.rs @@ -1,8 +1,6 @@ // errors4.rs // Execute `rustlings hint errors4` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - #[derive(PartialEq, Debug)] struct PositiveNonzeroInteger(u64); @@ -15,7 +13,14 @@ enum CreationError { impl PositiveNonzeroInteger { fn new(value: i64) -> Result { // Hmm...? Why is this only returning an Ok value? - Ok(PositiveNonzeroInteger(value as u64)) + if value > 0 { + Ok(PositiveNonzeroInteger(value as u64)) + } else if value == 0 { + Err(CreationError::Zero) + } else { + Err(CreationError::Negative) + } + } } diff --git a/exercises/error_handling/errors5.rs b/exercises/error_handling/errors5.rs index 6da06ef3..f93f705c 100644 --- a/exercises/error_handling/errors5.rs +++ b/exercises/error_handling/errors5.rs @@ -16,14 +16,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)?);