summaryrefslogtreecommitdiff
path: root/error_handling
diff options
context:
space:
mode:
authorCarol (Nichols || Goulding) <carol.nichols@gmail.com>2016-06-21 10:40:32 -0400
committerCarol (Nichols || Goulding) <carol.nichols@gmail.com>2016-06-21 10:40:32 -0400
commit0b15e927386ff1879e2892d017f2f1c845f4cb77 (patch)
tree35949837c442fdbf0a6058516d2257d1742c76e3 /error_handling
parentae46dfd752a8cd9af7c845aee62fb15bb29f3052 (diff)
Add another another error exercise
Diffstat (limited to 'error_handling')
-rw-r--r--error_handling/errors3.rs66
1 files changed, 66 insertions, 0 deletions
diff --git a/error_handling/errors3.rs b/error_handling/errors3.rs
new file mode 100644
index 0000000..3ecffbc
--- /dev/null
+++ b/error_handling/errors3.rs
@@ -0,0 +1,66 @@
+// This is a program that is trying to use a completed version of the
+// `total_cost` function from the previous exercise. It's not working though--
+// we can't call the `try!` macro in the `main()` function! Why not?
+// What should we do instead? Scroll for hints!
+
+use std::num::ParseIntError;
+
+fn main() {
+ let mut tokens = 100;
+ let pretend_user_input = "8";
+
+ let cost = try!(total_cost(pretend_user_input));
+
+ if cost > tokens {
+ println!("You can't afford that many!");
+ } else {
+ tokens -= cost;
+ println!("You now have {} tokens.", tokens);
+ }
+}
+
+pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
+ let processing_fee = 1;
+ let cost_per_item = 5;
+ let qty = try!(item_quantity.parse::<i32>());
+
+ Ok(qty * cost_per_item + processing_fee)
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+// Since the `try!` macro returns an `Err` early if the thing it's trying to
+// do fails, you can only use the `try!` macro in functions that have a
+// `Result` as their return type.
+
+// The error that you get if you run this code is:
+
+// ```
+// error: mismatched types:
+// expected `()`,
+// found `std::result::Result<_, _>`
+// ```
+
+// which is saying that the expected return type of the `main` function is
+// the empty tuple, but we tried to return a `Result`-- and that's happening
+// in the implementation of `try!`. The `main` function never has a return type,
+// so we have to use another way of handling a `Result` within `main`.
+
+// Decide what we should do if `pretend_user_input` has a string value that does
+// not parse to an integer, and implement that instead of calling the `try!`
+// macro.