From 32dd95cc03b9aa374b1b0a682fc3a6e6f2517545 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 22 Mar 2025 18:03:53 -0500 Subject: [PATCH] ch9 done --- ch9/recoverable-errors/Cargo.lock | 7 + ch9/recoverable-errors/Cargo.toml | 6 + ch9/recoverable-errors/hello.txt | 0 ch9/recoverable-errors/src/main.rs | 101 +++++++++++++++ ch9/to-panic-or-not-to-panic/Cargo.lock | 158 +++++++++++++++++++++++ ch9/to-panic-or-not-to-panic/Cargo.toml | 7 + ch9/to-panic-or-not-to-panic/src/main.rs | 45 +++++++ ch9/unrecoverable-errors/Cargo.lock | 7 + ch9/unrecoverable-errors/Cargo.toml | 6 + ch9/unrecoverable-errors/src/main.rs | 15 +++ 10 files changed, 352 insertions(+) create mode 100644 ch9/recoverable-errors/Cargo.lock create mode 100644 ch9/recoverable-errors/Cargo.toml create mode 100644 ch9/recoverable-errors/hello.txt create mode 100644 ch9/recoverable-errors/src/main.rs create mode 100644 ch9/to-panic-or-not-to-panic/Cargo.lock create mode 100644 ch9/to-panic-or-not-to-panic/Cargo.toml create mode 100644 ch9/to-panic-or-not-to-panic/src/main.rs create mode 100644 ch9/unrecoverable-errors/Cargo.lock create mode 100644 ch9/unrecoverable-errors/Cargo.toml create mode 100644 ch9/unrecoverable-errors/src/main.rs diff --git a/ch9/recoverable-errors/Cargo.lock b/ch9/recoverable-errors/Cargo.lock new file mode 100644 index 0000000..749bd2f --- /dev/null +++ b/ch9/recoverable-errors/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "recoverable-errors" +version = "0.1.0" diff --git a/ch9/recoverable-errors/Cargo.toml b/ch9/recoverable-errors/Cargo.toml new file mode 100644 index 0000000..c4c2d69 --- /dev/null +++ b/ch9/recoverable-errors/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "recoverable-errors" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch9/recoverable-errors/hello.txt b/ch9/recoverable-errors/hello.txt new file mode 100644 index 0000000..e69de29 diff --git a/ch9/recoverable-errors/src/main.rs b/ch9/recoverable-errors/src/main.rs new file mode 100644 index 0000000..06834e1 --- /dev/null +++ b/ch9/recoverable-errors/src/main.rs @@ -0,0 +1,101 @@ +// Uses the Result enum + +use std::error::Error; +use std::fs::{self, File}; +use std::io::{self, ErrorKind, Read}; + +// Can actually return a result. +// Need final line to be Ok(()) +fn main() -> Result<(), Box> { + let greeting_file_result = File::open("hello.txt"); + + // let greeting_file = match greeting_file_result { + // Ok(file) => file, + // Err(error) => panic!("Problem opening file: {error:?}"), + // }; + + // Matching on different errors + // let greeting_file = match greeting_file_result { + // Ok(file) => file, + // Err(error) => match error.kind() { + // std::io::ErrorKind::NotFound => match File::create("hello.txt") { + // Ok(fc) => fc, + // Err(e) => panic!("Problem creating file: {e:?}"), + // }, + // other_error => { + // panic!("Problem opening file: {other_error:?}"); + // } + // }, + // }; + + // Using closures this time + // let greeting_file = File::open("hello.txt").unwrap_or_else(|error| { + // if error.kind() == ErrorKind::NotFound { + // File::create("hello.txt").unwrap_or_else(|error| { + // panic!("Problem creating file: {error:?}"); + // }) + // } else { + // panic!("Problem opening file: {error:?}"); + // } + // }); + + // shortcuts for panic on error + // let greeting_file = File::open("hello.txt").unwrap(); + + let greeting_file = + File::open("hello.txt").expect("hello.txt should be included in this project"); + + // propagating errors + // instead of handling error within function itself, return + // error to the calling code so it can decide what to do + read_username_from_file(); + + // A shortcut for propapating errors: the ? Operator + read_username_from_file_question(); + + // The code below errors + // The ? can only be used in a function that returns Option or Result. + // Similar to raising into a Monad in haskell with the pure function in a do statement. + // let greeting_file = File::open("hello.txt")?; + + println!("Should be Some('a'): {:?}", last_char_of_first_line("line")); + println!("Should be None: {:?}", last_char_of_first_line("")); + + Ok(()) +} + +fn read_username_from_file() -> Result { + let username_file_result = File::open("hello.txt"); + + let mut username_file = match username_file_result { + Ok(file) => file, + Err(e) => return Err(e), + }; + + let mut username = String::new(); + + match username_file.read_to_string(&mut username) { + Ok(_) => Ok(username), + Err(e) => Err(e), + } +} + +fn read_username_from_file_question() -> Result { + // let mut username_file = File::open("hello.txt")?; + // let mut username = String::new(); + // username_file.read_to_string(&mut username)?; + // Ok(username) + + // Can chain function calls + // let mut username = String::new(); + // File::open("hello.txt")?.read_to_string(&mut username)?; + // Ok(username) + + // Even faster way to write this + // Just reads a file into a string + fs::read_to_string("hello.txt") +} + +fn last_char_of_first_line(text: &str) -> Option { + text.lines().next()?.chars().last() +} diff --git a/ch9/to-panic-or-not-to-panic/Cargo.lock b/ch9/to-panic-or-not-to-panic/Cargo.lock new file mode 100644 index 0000000..4cd3c82 --- /dev/null +++ b/ch9/to-panic-or-not-to-panic/Cargo.lock @@ -0,0 +1,158 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bitflags" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73fea8450eea4bac3940448fb7ae50d91f034f941199fcd9d909a5a07aa455f0" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.171" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5" + +[[package]] +name = "rand" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94" +dependencies = [ + "rand_chacha", + "rand_core", + "zerocopy", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" +dependencies = [ + "getrandom", +] + +[[package]] +name = "syn" +version = "2.0.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "to-panic-or-not-to-panic" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "wasi" +version = "0.14.2+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" +dependencies = [ + "wit-bindgen-rt", +] + +[[package]] +name = "wit-bindgen-rt" +version = "0.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" +dependencies = [ + "bitflags", +] + +[[package]] +name = "zerocopy" +version = "0.8.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2586fea28e186957ef732a5f8b3be2da217d65c5969d4b1e17f973ebbe876879" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a996a8f63c5c4448cd959ac1bab0aaa3306ccfd060472f85943ee0750f0169be" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/ch9/to-panic-or-not-to-panic/Cargo.toml b/ch9/to-panic-or-not-to-panic/Cargo.toml new file mode 100644 index 0000000..26128b8 --- /dev/null +++ b/ch9/to-panic-or-not-to-panic/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "to-panic-or-not-to-panic" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand = "0.9.0" diff --git a/ch9/to-panic-or-not-to-panic/src/main.rs b/ch9/to-panic-or-not-to-panic/src/main.rs new file mode 100644 index 0000000..c503f17 --- /dev/null +++ b/ch9/to-panic-or-not-to-panic/src/main.rs @@ -0,0 +1,45 @@ +use rand::Rng; +use std::cmp::Ordering; +use std::io; +use std::net::IpAddr; + +fn main() { + let home: IpAddr = "127.0.0.1" + .parse() + .expect("Hardcoded IP address should be valid"); + + // creating custom types for validation + + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1..=100); + + loop { + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + + let guess: i32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => continue, + }; + + if guess < 1 || guess > 100 { + println!("The secret number will be between 1 and 100."); + continue; + } + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => { + println!("You win!"); + break; + } + } + } +} diff --git a/ch9/unrecoverable-errors/Cargo.lock b/ch9/unrecoverable-errors/Cargo.lock new file mode 100644 index 0000000..cd8b0c5 --- /dev/null +++ b/ch9/unrecoverable-errors/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "unrecoverable-errors" +version = "0.1.0" diff --git a/ch9/unrecoverable-errors/Cargo.toml b/ch9/unrecoverable-errors/Cargo.toml new file mode 100644 index 0000000..804d235 --- /dev/null +++ b/ch9/unrecoverable-errors/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "unrecoverable-errors" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch9/unrecoverable-errors/src/main.rs b/ch9/unrecoverable-errors/src/main.rs new file mode 100644 index 0000000..48e6c9b --- /dev/null +++ b/ch9/unrecoverable-errors/src/main.rs @@ -0,0 +1,15 @@ +// Rust will try to clean up the program after a panic! is reached +// If want a smaller binary size, set: +// ```cargo.toml +// [profile.release] +// panic = 'abort' +// ``` +// to let the operating system handle the clean up. +// When Rust handles this, named unwinding +// +// Use RUST_BACKTRACE=1 %command% to see the full backtrace +// in case the panic isn't from your code directly. + +fn main() { + panic!("crash and burn"); +}