diff options
author | Adam Carpenter <gitlab@53hor.net> | 2019-07-09 15:14:04 -0400 |
---|---|---|
committer | Adam Carpenter <gitlab@53hor.net> | 2019-07-09 15:14:04 -0400 |
commit | 7e8ee5ed9cad6484e9f13f81731b102ced58402e (patch) | |
tree | 5395402ab07bbb5a659dbd68c701e22a1227202f /rust-book/errors/src | |
download | learning-rust-7e8ee5ed9cad6484e9f13f81731b102ced58402e.tar.xz learning-rust-7e8ee5ed9cad6484e9f13f81731b102ced58402e.zip |
Init.
Diffstat (limited to 'rust-book/errors/src')
-rwxr-xr-x | rust-book/errors/src/main.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/rust-book/errors/src/main.rs b/rust-book/errors/src/main.rs new file mode 100755 index 0000000..1250677 --- /dev/null +++ b/rust-book/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()); +} |