From 64d95837e9813541cf5b357de13865ce687ae98d Mon Sep 17 00:00:00 2001 From: Adam Brewer Date: Mon, 16 Oct 2023 07:37:12 -0400 Subject: Update Exercises Directory Names to Reflect Order --- exercises/error_handling/errors2.rs | 50 ------------------------------------- 1 file changed, 50 deletions(-) delete mode 100644 exercises/error_handling/errors2.rs (limited to 'exercises/error_handling/errors2.rs') diff --git a/exercises/error_handling/errors2.rs b/exercises/error_handling/errors2.rs deleted file mode 100644 index 631fe67..0000000 --- a/exercises/error_handling/errors2.rs +++ /dev/null @@ -1,50 +0,0 @@ -// errors2.rs -// -// 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, though, we get it as a string-- and 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`, and 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! -// -// 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::(); - - Ok(qty * cost_per_item + processing_fee) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[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().to_string(), - "invalid digit found in string" - ); - } -} -- cgit v1.2.3