From 050a23ce6763fedf0906cd1c04b76888aae12f7d Mon Sep 17 00:00:00 2001 From: mo8it Date: Wed, 26 Jun 2024 15:36:14 +0200 Subject: [PATCH] errors2 solution --- exercises/13_error_handling/errors2.rs | 19 +++++---- rustlings-macros/info.toml | 7 ++-- solutions/13_error_handling/errors2.rs | 58 +++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 13 deletions(-) diff --git a/exercises/13_error_handling/errors2.rs b/exercises/13_error_handling/errors2.rs index 345a0eef..e50a9299 100644 --- a/exercises/13_error_handling/errors2.rs +++ b/exercises/13_error_handling/errors2.rs @@ -2,16 +2,16 @@ // 5 tokens, and whenever you purchase items there is a processing fee of 1 // token. A player of the game will type in how many items they want to buy, and // the `total_cost` function will calculate the total cost of the items. Since -// the player typed in the quantity, though, we get it as a string-- and they -// might have typed anything, not just numbers! +// the player typed in the quantity, we get it as a string. They might have +// typed anything, not just numbers! // // Right now, this function isn't handling the error case at all (and isn't -// handling the success case properly either). What we want to do is: if we call +// handling the success case properly either). What we want to do is: If we call // the `total_cost` function on a string that is not a number, that function -// will return a `ParseIntError`, and in that case, we want to immediately -// return that error from our function and not try to multiply and add. +// will return a `ParseIntError`. In that case, we want to immediately return +// that error from our function and not try to multiply and add. // -// There are at least two ways to implement this that are both correct-- but one +// There are at least two ways to implement this that are both correct. But one // is a lot shorter! use std::num::ParseIntError; @@ -19,6 +19,8 @@ use std::num::ParseIntError; fn total_cost(item_quantity: &str) -> Result { let processing_fee = 1; let cost_per_item = 5; + + // TODO: Handle the error case as described above. let qty = item_quantity.parse::(); Ok(qty * cost_per_item + processing_fee) @@ -31,6 +33,7 @@ fn main() { #[cfg(test)] mod tests { use super::*; + use std::num::IntErrorKind; #[test] fn item_quantity_is_a_valid_number() { @@ -40,8 +43,8 @@ mod tests { #[test] fn item_quantity_is_an_invalid_number() { assert_eq!( - total_cost("beep boop").unwrap_err().to_string(), - "invalid digit found in string" + total_cost("beep boop").unwrap_err().kind(), + &IntErrorKind::InvalidDigit, ); } } diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml index 3d8da58f..2a4a24ea 100644 --- a/rustlings-macros/info.toml +++ b/rustlings-macros/info.toml @@ -660,12 +660,11 @@ One way to handle this is using a `match` statement on `item_quantity.parse::()` where the cases are `Ok(something)` and `Err(something)`. -This pattern is very common in Rust, though, so there's a `?` operator that +This pattern is very common in Rust, though, so there's the `?` operator that does pretty much what you would make that match statement do for you! -Take a look at this section of the 'Error Handling' chapter: -https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator -and give it a try!""" +Take a look at this section of the "Error Handling" chapter: +https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator""" [[exercises]] name = "errors3" diff --git a/solutions/13_error_handling/errors2.rs b/solutions/13_error_handling/errors2.rs index 4e181989..de7c32b5 100644 --- a/solutions/13_error_handling/errors2.rs +++ b/solutions/13_error_handling/errors2.rs @@ -1 +1,57 @@ -// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 +// Say we're writing a game where you can buy items with tokens. All items cost +// 5 tokens, and whenever you purchase items there is a processing fee of 1 +// token. A player of the game will type in how many items they want to buy, and +// the `total_cost` function will calculate the total cost of the items. Since +// the player typed in the quantity, we get it as a string. They might have +// typed anything, not just numbers! +// +// Right now, this function isn't handling the error case at all (and isn't +// handling the success case properly either). What we want to do is: If we call +// the `total_cost` function on a string that is not a number, that function +// will return a `ParseIntError`. In that case, we want to immediately return +// that error from our function and not try to multiply and add. +// +// There are at least two ways to implement this that are both correct. But one +// is a lot shorter! + +use std::num::ParseIntError; + +fn total_cost(item_quantity: &str) -> Result { + let processing_fee = 1; + let cost_per_item = 5; + + // Added `?` to propagate the error. + let qty = item_quantity.parse::()?; + // ^ added + + // Equivalent to this verbose version: + let qty = match item_quantity.parse::() { + Ok(v) => v, + Err(e) => return Err(e), + }; + + Ok(qty * cost_per_item + processing_fee) +} + +fn main() { + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + use super::*; + use std::num::IntErrorKind; + + #[test] + fn item_quantity_is_a_valid_number() { + assert_eq!(total_cost("34"), Ok(171)); + } + + #[test] + fn item_quantity_is_an_invalid_number() { + assert_eq!( + total_cost("beep boop").unwrap_err().kind(), + &IntErrorKind::InvalidDigit, + ); + } +}