From 6b0ff3756228e9b186e0fc37c229052854a46c56 Mon Sep 17 00:00:00 2001 From: Reality Enjoyer Date: Sat, 17 Jan 2026 09:01:51 +0000 Subject: [PATCH] start error handling --- {average_speed => average-speed}/Cargo.toml | 0 {average_speed => average-speed}/src/main.rs | 0 error-handling/Cargo.toml | 6 +++ error-handling/hello.txt | 0 error-handling/src/main.rs | 39 ++++++++++++++++++++ error-handling/username.txt | 0 error-handling/{file_created} | 1 + {guessing_game => guessing-game}/Cargo.toml | 0 {guessing_game => guessing-game}/src/main.rs | 0 {hash_maps => hash-maps}/Cargo.toml | 0 {hash_maps => hash-maps}/src/main.rs | 0 {hello_cargo => hello-cargo}/Cargo.toml | 0 {hello_cargo => hello-cargo}/src/main.rs | 0 {hello_world => hello-world}/main.rs | 0 14 files changed, 46 insertions(+) rename {average_speed => average-speed}/Cargo.toml (100%) rename {average_speed => average-speed}/src/main.rs (100%) create mode 100644 error-handling/Cargo.toml create mode 100644 error-handling/hello.txt create mode 100644 error-handling/src/main.rs create mode 100644 error-handling/username.txt create mode 100644 error-handling/{file_created} rename {guessing_game => guessing-game}/Cargo.toml (100%) rename {guessing_game => guessing-game}/src/main.rs (100%) rename {hash_maps => hash-maps}/Cargo.toml (100%) rename {hash_maps => hash-maps}/src/main.rs (100%) rename {hello_cargo => hello-cargo}/Cargo.toml (100%) rename {hello_cargo => hello-cargo}/src/main.rs (100%) rename {hello_world => hello-world}/main.rs (100%) diff --git a/average_speed/Cargo.toml b/average-speed/Cargo.toml similarity index 100% rename from average_speed/Cargo.toml rename to average-speed/Cargo.toml diff --git a/average_speed/src/main.rs b/average-speed/src/main.rs similarity index 100% rename from average_speed/src/main.rs rename to average-speed/src/main.rs diff --git a/error-handling/Cargo.toml b/error-handling/Cargo.toml new file mode 100644 index 0000000..9a1ff56 --- /dev/null +++ b/error-handling/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "error_handling" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/error-handling/hello.txt b/error-handling/hello.txt new file mode 100644 index 0000000..e69de29 diff --git a/error-handling/src/main.rs b/error-handling/src/main.rs new file mode 100644 index 0000000..a6a97e4 --- /dev/null +++ b/error-handling/src/main.rs @@ -0,0 +1,39 @@ +use std::fs::{self, File}; +use std::io::{self, Read}; + +fn main() { + let username = get_username_or_create_file().unwrap(); + if username == "" { + println!("empty!"); + } else { + println!("{:?}", username); + } +} + +fn get_username_or_create_file() -> Result { + let check_username_file = File::open("username.txt"); + + let mut username_file = match check_username_file { + Ok(filename) => filename, + Err(e) => match e.kind() { + std::io::ErrorKind::NotFound => match File::create("username.txt") { + Ok(file_created) => { + println!("created file, now writing..."); + fs::write("{file_created}", "admin"); + file_created + } + Err(error_creating_file) => { + panic!("error creating file! {error_creating_file:?}") + } + }, + _ => panic!("cant open file!"), + }, + }; + + let mut username = String::new(); + + match username_file.read_to_string(&mut username) { + Ok(_) => Ok(username), + Err(e) => Err(e), + } +} diff --git a/error-handling/username.txt b/error-handling/username.txt new file mode 100644 index 0000000..e69de29 diff --git a/error-handling/{file_created} b/error-handling/{file_created} new file mode 100644 index 0000000..f77b004 --- /dev/null +++ b/error-handling/{file_created} @@ -0,0 +1 @@ +admin \ No newline at end of file diff --git a/guessing_game/Cargo.toml b/guessing-game/Cargo.toml similarity index 100% rename from guessing_game/Cargo.toml rename to guessing-game/Cargo.toml diff --git a/guessing_game/src/main.rs b/guessing-game/src/main.rs similarity index 100% rename from guessing_game/src/main.rs rename to guessing-game/src/main.rs diff --git a/hash_maps/Cargo.toml b/hash-maps/Cargo.toml similarity index 100% rename from hash_maps/Cargo.toml rename to hash-maps/Cargo.toml diff --git a/hash_maps/src/main.rs b/hash-maps/src/main.rs similarity index 100% rename from hash_maps/src/main.rs rename to hash-maps/src/main.rs diff --git a/hello_cargo/Cargo.toml b/hello-cargo/Cargo.toml similarity index 100% rename from hello_cargo/Cargo.toml rename to hello-cargo/Cargo.toml diff --git a/hello_cargo/src/main.rs b/hello-cargo/src/main.rs similarity index 100% rename from hello_cargo/src/main.rs rename to hello-cargo/src/main.rs diff --git a/hello_world/main.rs b/hello-world/main.rs similarity index 100% rename from hello_world/main.rs rename to hello-world/main.rs