summaryrefslogtreecommitdiff
path: root/error_handling
diff options
context:
space:
mode:
authorCarol (Nichols || Goulding) <carol.nichols@gmail.com>2016-02-16 17:43:44 -0500
committerCarol (Nichols || Goulding) <carol.nichols@gmail.com>2016-04-16 10:53:39 -0400
commit4587266b828f2ad1d594dcd747ac17ba9ea4772c (patch)
tree4e6d036cbb94f6ea7d4027d44463db7ba319bed2 /error_handling
parentd145c6334f34fb4a1289368175d73aeaa629090c (diff)
Rename the first errors exercise to be about Option, link to it
Diffstat (limited to 'error_handling')
-rw-r--r--error_handling/option1.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/error_handling/option1.rs b/error_handling/option1.rs
new file mode 100644
index 0000000..5f96184
--- /dev/null
+++ b/error_handling/option1.rs
@@ -0,0 +1,44 @@
+// This example panics because the second time it calls `pop`, the `vec`
+// is empty, so `pop` returns `None`, and `unwrap` panics if it's called
+// on `None`. Handle this in a more graceful way than calling `unwrap`!
+// Scroll down for hints :)
+
+fn main() {
+ let mut list = vec![3];
+
+ let last = list.pop().unwrap();
+ println!("The last item in the list is {:?}", last);
+
+ let second_to_last = list.pop().unwrap();
+ println!("The second-to-last item in the list is {:?}", second_to_last);
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+// Try using a `match` statement where the arms are `Some(thing)` and `None`.
+// Or set a default value to print out if you get `None` by using the
+// function `unwrap_or`.
+// Or use an `if let` statement on the result of `pop()` to both destructure
+// a `Some` value and only print out something if we have a value!