From eeb7ceeff94b9bac0fb629ad393e7113a04f31c1 Mon Sep 17 00:00:00 2001 From: Jordan Gong Date: Tue, 1 Sep 2020 20:32:27 +0800 Subject: Handle errors --- errors/.gitignore | 18 ++++++++++++ errors/Cargo.toml | 9 ++++++ errors/src/main.rs | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 errors/.gitignore create mode 100644 errors/Cargo.toml create mode 100644 errors/src/main.rs diff --git a/errors/.gitignore b/errors/.gitignore new file mode 100644 index 0000000..e629269 --- /dev/null +++ b/errors/.gitignore @@ -0,0 +1,18 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/rust +# Edit at https://www.toptal.com/developers/gitignore?templates=rust + +### Rust ### +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# End of https://www.toptal.com/developers/gitignore/api/rust + diff --git a/errors/Cargo.toml b/errors/Cargo.toml new file mode 100644 index 0000000..b7a199b --- /dev/null +++ b/errors/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "errors" +version = "0.1.0" +authors = ["Jordan Gong "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/errors/src/main.rs b/errors/src/main.rs new file mode 100644 index 0000000..e98e987 --- /dev/null +++ b/errors/src/main.rs @@ -0,0 +1,83 @@ +use std::fs::File; +use std::io::ErrorKind; + +fn main() { + // panic!("crash and burn"); + + // 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!("Problem creating the file: {:?}", e), + // }, + // other_error => { + // panic!("Problem opening file: {:?}", other_error) + // } + // }, + // }; + + // Rustacean style + // let f = File::open("hello.txt").unwrap_or_else(|error| { + // if error.kind() == ErrorKind::NotFound { + // File::create("hello.txt").unwrap_or_else(|error| { + // panic!("Problem creating the file: {:?}", error); + // }) + // } else { + // panic!("Problem opening file: {:?}", error); + // } + // }); + + // Shortcut + // let f = File::open("hello.txt").unwrap(); + + // let f = File::open("hello.txt").expect("Failed to open hello.txt"); + + // Propagating + use std::io; + use std::io::Read; + + // fn read_username_from_file() -> Result { + // let f = File::open("hello.txt"); + + // let mut f = match f { + // Ok(file) => file, + // Err(e) => return Err(e), + // }; + + // let mut s = String::new(); + + // match f.read_to_string(&mut s) { + // Ok(_) => Ok(s), + // Err(e) => Err(e), + // } + // } + + // Propagating shortcut + // fn read_username_from_file() -> Result { + // let mut f = File::open("hello.txt")?; + // let mut s = String::new(); + // f.read_to_string(&mut s)?; + // Ok(s) + // } + + // Chaining + // fn read_username_from_file() -> Result { + // let mut s = String::new(); + + // File::open("hello.txt")?.read_to_string(&mut s)?; + + // Ok(s) + // } + + // Using `fs::read_to_string` + use std::fs; + fn read_username_from_file() -> Result { + fs::read_to_string("hello.txt") + } + + let r = read_username_from_file(); + println!("{:?}", r); +} -- cgit v1.2.3