From 2e599425557cb18dbef982907634b8dc5a0d76ba Mon Sep 17 00:00:00 2001 From: Reality Enjoyer Date: Sat, 17 Jan 2026 16:37:23 +0000 Subject: [PATCH] update error handling example - replaced `match`s with `?`s where possible - it should not be considered an improvement tho - turns out `?` is best used when you are returning `Result`s - we were returning `String`s - so we had to wrap all successful string returns in `Ok()`s - which is just clunky - good learning but its time to move on! --- error-handling/src/main.rs | 66 +++++++++----------------------------- 1 file changed, 15 insertions(+), 51 deletions(-) diff --git a/error-handling/src/main.rs b/error-handling/src/main.rs index db0b71b..c2e1f31 100644 --- a/error-handling/src/main.rs +++ b/error-handling/src/main.rs @@ -2,7 +2,6 @@ use std::fs::{self, File}; use std::io::{self, Read}; fn main() { - // read file or create it if it doesnt exist match read_or_create_usernames_file() { Ok(_) => (), Err(e) => { @@ -11,74 +10,39 @@ fn main() { } } - // print users - let usernames = get_or_set_usernames(); - + let usernames = get_or_set_usernames().unwrap(); println!("printing usernames:\n{usernames:?}"); } -fn get_or_set_usernames() -> String { +fn get_or_set_usernames() -> Result { let usernames_file = File::open("usernames.txt"); - let mut usernames = String::new(); - match usernames_file.unwrap().read_to_string(&mut usernames) { - Ok(_) => (), - Err(e) => return format!("could not read from file!\n{e:?}").to_string(), - } + let mut usernames_file_copy = File::open("usernames.txt")?; + let mut usernames = String::new(); + usernames_file_copy.read_to_string(&mut usernames)?; if !usernames.is_empty() { - return usernames; + return Ok(usernames); } else { println!("file is empty! adding the admin user"); - let add_first_user = fs::write("usernames.txt", "admin"); - match add_first_user { - Ok(_) => { - println!("added admin"); - } - - Err(e) => { - println!( - "could not add the admin user - error: {e:?}" - ); - return format!( - "file is empty and we could not add the admin user - {e:?}" - ) - .to_string(); - } - } + fs::write("usernames.txt", "admin")?; } - let usernames_file = File::open("usernames.txt"); + // here lies the clunky part + File::open("usernames.txt")?; let mut usernames = String::new(); - - match usernames_file.unwrap().read_to_string(&mut usernames) { - Ok(_) => (), - Err(e) => return format!("could not read from file after adding admin\n{e:?}").to_string(), - } - usernames + usernames_file.unwrap().read_to_string(&mut usernames)?; + Ok(usernames) } -fn read_or_create_usernames_file() -> Result { +fn read_or_create_usernames_file() -> Result { let file_exists = File::open("usernames.txt"); - match file_exists { - Ok(filename) => return Ok(filename), + Ok(_) => (), Err(e) => { println!("file does not exist!\n{e:?}\ncreating usernames.txt"); - - let create_file = File::create("usernames.txt"); - - match create_file { - Ok(filename) => { - println!("created file: {filename:?}"); - return Ok(filename); - } - Err(e) => { - return Err(e); - } - } + File::create("usernames.txt")?; } } + return Ok(0); }