2018-02-22 06:09:53 +00:00
|
|
|
// errors2.rs
|
2023-05-29 17:39:08 +00:00
|
|
|
//
|
2016-06-21 14:07:21 +00:00
|
|
|
// 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
|
2023-05-29 17:39:08 +00:00
|
|
|
// token. A player of the game will type in how many items they want to buy, and
|
2023-10-13 19:47:38 +00:00
|
|
|
// the `total_cost` function will calculate the total cost of the items. Since
|
2023-05-29 17:39:08 +00:00
|
|
|
// the player typed in the quantity, though, we get it as a string-- and they
|
|
|
|
// might have typed anything, not just numbers!
|
|
|
|
//
|
2016-06-21 14:07:21 +00:00
|
|
|
// Right now, this function isn't handling the error case at all (and isn't
|
2023-05-29 17:39:08 +00:00
|
|
|
// handling the success case properly either). What we want to do is: if we call
|
2023-08-14 19:49:28 +00:00
|
|
|
// 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.
|
2023-05-29 17:39:08 +00:00
|
|
|
//
|
|
|
|
// 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.
|
2016-06-21 14:07:21 +00:00
|
|
|
|
2019-11-11 12:38:24 +00:00
|
|
|
// I AM NOT DONE
|
|
|
|
|
2016-06-21 14:07:21 +00:00
|
|
|
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>();
|
|
|
|
|
|
|
|
Ok(qty * cost_per_item + processing_fee)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn item_quantity_is_a_valid_number() {
|
2019-05-22 11:48:32 +00:00
|
|
|
assert_eq!(total_cost("34"), Ok(171));
|
2016-06-21 14:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn item_quantity_is_an_invalid_number() {
|
|
|
|
assert_eq!(
|
|
|
|
total_cost("beep boop").unwrap_err().to_string(),
|
|
|
|
"invalid digit found in string"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|