From 83fc3600b8cbbfe4204cea80e3e1385f41c98a93 Mon Sep 17 00:00:00 2001 From: Reality Enjoyer Date: Fri, 19 Dec 2025 07:21:37 +0000 Subject: [PATCH] getting started --- guessing_game/Cargo.toml | 7 ++++++ guessing_game/src/main.rs | 45 +++++++++++++++++++++++++++++++++++++++ hello_cargo/Cargo.toml | 6 ++++++ hello_cargo/src/main.rs | 3 +++ hello_world/main.rs | 3 +++ 5 files changed, 64 insertions(+) create mode 100644 guessing_game/Cargo.toml create mode 100644 guessing_game/src/main.rs create mode 100644 hello_cargo/Cargo.toml create mode 100644 hello_cargo/src/main.rs create mode 100644 hello_world/main.rs diff --git a/guessing_game/Cargo.toml b/guessing_game/Cargo.toml new file mode 100644 index 0000000..9f9c4ac --- /dev/null +++ b/guessing_game/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2024" + +[dependencies] +rand = "0.8.5" diff --git a/guessing_game/src/main.rs b/guessing_game/src/main.rs new file mode 100644 index 0000000..dcc74d8 --- /dev/null +++ b/guessing_game/src/main.rs @@ -0,0 +1,45 @@ +// rewriting the guessing game myself +// pseudocode: +// DONE 1. generate a secret number +// DONE 2. take input from user +// DONE 3. validate the input +// DONE 4. compare the input +// DONE 5. give feedback to user (guess is too big or small) +// DONE 6. if their guess is correct, say so and quit the program + +use rand::{Rng, thread_rng}; +use std::io; + +fn main() { + let secret = thread_rng().gen_range(0..10); + + println!("welcome to the guessing game!"); + println!("we have selected a number from 1 to 10"); + + loop { + let mut guess = String::new(); + + println!("\nwhat's the number? "); + + io::stdin() + .read_line(&mut guess) + .expect("failed to read line!"); + + let guess: u32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => { + println!("WARN: please only enter numbers!"); + continue; + } + }; + + if guess > secret { + println!("your guess {} is higher!", guess); + } else if guess < secret { + println!("your guess {} is lower!", guess); + } else { + println!("that's right!"); + break; + } + } +} diff --git a/hello_cargo/Cargo.toml b/hello_cargo/Cargo.toml new file mode 100644 index 0000000..f43e5f8 --- /dev/null +++ b/hello_cargo/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello_cargo" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/hello_cargo/src/main.rs b/hello_cargo/src/main.rs new file mode 100644 index 0000000..e0b5e27 --- /dev/null +++ b/hello_cargo/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello world!"); +} diff --git a/hello_world/main.rs b/hello_world/main.rs new file mode 100644 index 0000000..e0b5e27 --- /dev/null +++ b/hello_world/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello world!"); +}