1// Say we're writing a game where you can buy items with tokens. All items cost
2// 5 tokens, and whenever you purchase items there is a processing fee of 1
3// token. A player of the game will type in how many items they want to buy, and
4// the `total_cost` function will calculate the total cost of the items. Since
5// the player typed in the quantity, we get it as a string. They might have
6// typed anything, not just numbers!
7//
8// Right now, this function isn't handling the error case at all. What we want
9// to do is: If we call the `total_cost` function on a string that is not a
10// number, that function will return a `ParseIntError`. In that case, we want to
11// immediately return that error from our function and not try to multiply and
12// add.
13//
14// There are at least two ways to implement this that are both correct. But one
15// is a lot shorter!
16
17use std::num::ParseIntError;
18
19#[allow(unused_variables)]
20fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
21 let processing_fee = 1;
22 let cost_per_item = 5;
23
24 // Added `?` to propagate the error.
25 let qty = item_quantity.parse::<i32>()?;
26 // ^ added
27
28 // Equivalent to this verbose version:
29 let qty = match item_quantity.parse::<i32>() {
30 Ok(v) => v,
31 Err(e) => return Err(e),
32 };
33
34 Ok(qty * cost_per_item + processing_fee)
35}
36
37fn main() {
38 // You can optionally experiment here.
39}
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44 use std::num::IntErrorKind;
45
46 #[test]
47 fn item_quantity_is_a_valid_number() {
48 assert_eq!(total_cost("34"), Ok(171));
49 }
50
51 #[test]
52 fn item_quantity_is_an_invalid_number() {
53 assert_eq!(
54 total_cost("beep boop").unwrap_err().kind(),
55 &IntErrorKind::InvalidDigit,
56 );
57 }
58}