diff --git a/.gitignore b/.gitignore
index 2b1665f..dab9a24 100644
--- a/.gitignore
+++ b/.gitignore
@@ -42,3 +42,5 @@ Cargo.lock
**/*.rs.bk
# End of https://www.gitignore.io/api/osx,rust
+
+errors/hello.txt
diff --git a/.idea/misc.xml b/.idea/misc.xml
index b7bb25f..baf7791 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -15,6 +15,7 @@
+
diff --git a/.idea/rust.iml b/.idea/rust.iml
index 88c150b..a8e328f 100644
--- a/.idea/rust.iml
+++ b/.idea/rust.iml
@@ -59,9 +59,14 @@
+
+
+
+
+
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index f30eaf6..145a8ab 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -3,6 +3,9 @@
+
+
+
@@ -13,10 +16,14 @@
-
+
-
-
+
+
+
+
+
+
@@ -24,8 +31,8 @@
-
-
+
+
@@ -70,6 +77,7 @@
+
@@ -101,6 +109,11 @@
+
+
+
+
+
@@ -132,7 +145,7 @@
-
+
@@ -160,7 +173,7 @@
-
+
@@ -189,6 +202,16 @@
+
+
+
+
+
+
+
+
+
+
@@ -209,16 +232,6 @@
-
-
-
-
-
-
-
-
-
-
@@ -238,11 +251,11 @@
+
-
@@ -258,7 +271,7 @@
-
+
@@ -438,10 +451,24 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
diff --git a/errors/Cargo.toml b/errors/Cargo.toml
new file mode 100644
index 0000000..af17f0a
--- /dev/null
+++ b/errors/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "errors"
+version = "0.1.0"
+authors = ["Timothy Warren "]
+edition = "2018"
+
+[dependencies]
diff --git a/errors/src/main.rs b/errors/src/main.rs
new file mode 100644
index 0000000..de99af6
--- /dev/null
+++ b/errors/src/main.rs
@@ -0,0 +1,65 @@
+use std::fs;
+use std::fs::File;
+use std::io::{Error, ErrorKind, Read};
+
+fn main() {
+ let _str = handle_errors_with_match();
+
+ let _str = match handle_errors_with_closures() {
+ Ok(_) => println!("Opened or created the file successfully!"),
+ Err(e) => panic!("There was a problem opening the file: {:?}", e),
+ };
+
+ let _str = match read_username_from_file() {
+ Ok(_) => println!("Opened or created the file successfully!"),
+ Err(e) => panic!("There was a problem opening the file: {:?}", e),
+ };
+
+ let _str = match easy_mode() {
+ Ok(_) => println!("Opened or created the file successfully!"),
+ Err(e) => panic!("There was a problem opening the file: {:?}", e),
+ };
+}
+
+fn easy_mode() -> Result {
+ fs::read_to_string("hello.txt")
+}
+
+fn read_username_from_file() -> Result {
+ let mut s = String::new();
+
+ File::open("hello.txt")?.read_to_string(&mut s)?;
+
+ Ok(s)
+}
+
+fn handle_errors_with_closures () -> Result {
+ File::open("hello.txt").map_err(|error| {
+ if error.kind() == ErrorKind::NotFound {
+ File::create("hello.txt").unwrap_or_else(|error| {
+ panic!("Tried to create file but there was a problem: {:?}", error);
+ })
+ } else {
+ panic!("There was a problem opening the file: {:?}", error);
+ }
+ })
+}
+
+fn handle_errors_with_match () -> File {
+ let f = File::open("hello.txt");
+
+ let f = match f {
+ Ok(file) => file,
+ Err(error) => match error.kind() {
+ ErrorKind::NotFound => match File::create("hello.txt") {
+ Ok(fc) => fc,
+ Err(e) => panic!("Tried to create file but there was a problem: {:?}", e),
+ },
+ other_error => panic!("There was a problem opening the file: {:?}", other_error),
+ },
+ };
+
+ println!("Opened or created the file successfully!");
+
+ f
+}