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