From f63775945439f392b30c937271259c6d86a06a25 Mon Sep 17 00:00:00 2001 From: laurens Date: Sun, 21 Jun 2020 17:47:35 +0200 Subject: [PATCH] Example on error handling and checking error types --- errors/Cargo.toml | 9 ++++++ errors/src/main.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 errors/Cargo.toml create mode 100644 errors/src/main.rs diff --git a/errors/Cargo.toml b/errors/Cargo.toml new file mode 100644 index 0000000..fa1c748 --- /dev/null +++ b/errors/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "errors" +version = "0.1.0" +authors = ["laurens "] +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..5e7cb81 --- /dev/null +++ b/errors/src/main.rs @@ -0,0 +1,71 @@ +use std::fs; +use std::fs::File; +use std::io::ErrorKind; +use std::io; +use std::io::Read; +use std::error::Error; + +fn open_and_close_with_match(path: &String) -> File { + // Big block of code with match-expressions for nothing really + let f = match File::open(path) { + Ok(file) => file, + Err(error) => match error.kind() { + ErrorKind::NotFound => match File::create(path) { + Ok(fc) => fc, + Err(e) => panic!("Problem creating the file: {:?}", e), + }, + other_error => panic!("Problem opening the file: {:?}", other_error), + }, + }; + + return f; +} + +fn open_and_close_without_match(path: &String) -> File { + let f = File::open(path).unwrap_or_else(|error| { + if error.kind() == ErrorKind::NotFound { + File::create(path).unwrap_or_else(|error| { + panic!("Failed to create file {:?}", error); + }) + } else { + panic!("Problem opening file {:?}", error); + } + }); + + return f; +} + +fn read_username_from_file(path: &String) -> Result { + let mut f = File::open(path)?; + let mut s = String::new(); + f.read_to_string(&mut s)?; + Ok(s) +} + +fn main() -> Result<(), Box> { + let path = "hello.txt".to_string(); + + println!("Open/create file {}", path); + open_and_close_with_match(&path); + + // If we get here, the file surely exists + println!("Remove file {}", path); + fs::remove_file(&path).expect("Failed to remove file"); + + println!("Open/create file {}", path); + open_and_close_without_match(&path); + + // If we get here, the file surely exists + println!("Remove file {}", path); + fs::remove_file(&path).expect("Failed to remove file"); + + let username = read_username_from_file(&path).expect("Could not read username from file"); + + println!("username: {}", username); + + // Returning error out of main + let _f = File::open("hello.txt")?; + fs::remove_file(&path).expect("Failed to remove file"); + + Ok(()) +}