diff options
author | Adam Carpenter <adam.carpenter@adp.com> | 2019-02-11 16:59:01 -0500 |
---|---|---|
committer | Adam Carpenter <adam.carpenter@adp.com> | 2019-02-11 16:59:01 -0500 |
commit | ef1bf4921ee4127d461eec03a14c9070d193345c (patch) | |
tree | ab9f4ed73fcf97656743c959ce8c9adbe2ce9924 /errors | |
download | learning-rust-ef1bf4921ee4127d461eec03a14c9070d193345c.tar.xz learning-rust-ef1bf4921ee4127d461eec03a14c9070d193345c.zip |
Init.
Diffstat (limited to 'errors')
-rw-r--r-- | errors/.gitignore | 2 | ||||
-rw-r--r-- | errors/Cargo.lock | 4 | ||||
-rw-r--r-- | errors/Cargo.toml | 7 | ||||
-rw-r--r-- | errors/src/main.rs | 38 |
4 files changed, 51 insertions, 0 deletions
diff --git a/errors/.gitignore b/errors/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/errors/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/errors/Cargo.lock b/errors/Cargo.lock new file mode 100644 index 0000000..749984f --- /dev/null +++ b/errors/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "errors" +version = "0.1.0" + diff --git a/errors/Cargo.toml b/errors/Cargo.toml new file mode 100644 index 0000000..e531a1b --- /dev/null +++ b/errors/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "errors" +version = "0.1.0" +authors = ["Adam Carpenter <adam.carpenter@adp.com>"] +edition = "2018" + +[dependencies] diff --git a/errors/src/main.rs b/errors/src/main.rs new file mode 100644 index 0000000..1250677 --- /dev/null +++ b/errors/src/main.rs @@ -0,0 +1,38 @@ +use std::io; +use std::io::Read; +use std::fs::File; +//use std::io::ErrorKind; + +fn read_username_from_file() -> Result<String, io::Error> { +// 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), +// } + let mut s = String::new(); + File::open("hello.txt")?.read_to_string(&mut s)?; + Ok(s) +} +fn main() { +// let f = File::open("hello.txt").map_err(|error| { +// if error.kind() == ErrorKind::NotFound { +// File::create("hello.txt").unwrap_or_else(|error| { +// panic!("Could not create file: {:?}", error); +// }) +// } +// else { +// panic!("Problem opening file: {:?}", error); +// } +// }); + + //let f = File::open("hello.txt").unwrap(); + + //let f = File::open("hello.txt").expect("failed to open hello.txt"); + + dbg!(read_username_from_file()); +} |