summaryrefslogtreecommitdiff
path: root/exercises/13_error_handling/errors2.rs
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-07-05 13:39:50 +0200
committermo8it <mo8it@proton.me>2024-07-05 13:39:50 +0200
commit7123c7ae3a9605fbe962e4ef0a0f1424cd16fef8 (patch)
treec67f7e62bb9a179ae4fdbab492501cb6847e64c7 /exercises/13_error_handling/errors2.rs
parent77b687d501771c24bd83294d97b8e6f9ffa92d6b (diff)
parent4d9c346a173bb722b929f3ea3c00f84954483e24 (diff)
Merge remote-tracking branch 'upstream/main' into fix-enum-variant-inconsistency
Diffstat (limited to 'exercises/13_error_handling/errors2.rs')
-rw-r--r--exercises/13_error_handling/errors2.rs36
1 files changed, 18 insertions, 18 deletions
diff --git a/exercises/13_error_handling/errors2.rs b/exercises/13_error_handling/errors2.rs
index 631fe67..defe359 100644
--- a/exercises/13_error_handling/errors2.rs
+++ b/exercises/13_error_handling/errors2.rs
@@ -1,39 +1,39 @@
-// 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!
+// 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`, and in that case, we want to immediately
-// return that error from our function and not try to multiply and add.
+// Right now, this function isn't handling the error case at all. 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
+// 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<i32, ParseIntError> {
+fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
let processing_fee = 1;
let cost_per_item = 5;
+
+ // TODO: Handle the error case as described above.
let qty = item_quantity.parse::<i32>();
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() {
@@ -43,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,
);
}
}