summaryrefslogtreecommitdiff
path: root/rust/guessing_game/src
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-07-10 22:24:20 +0200
committerCarlos Maiolino <[email protected]>2025-07-10 22:24:20 +0200
commit869e68986aa8f69af6e7842260a68d1e5c6f796f (patch)
tree63b6b5ffc3d19414233d4629a533c0d9bf3cbf72 /rust/guessing_game/src
parent20834dcc57537cd95260a4a22f5d91a027adfd35 (diff)
Add a bunch of code
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'rust/guessing_game/src')
-rw-r--r--rust/guessing_game/src/main.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/rust/guessing_game/src/main.rs b/rust/guessing_game/src/main.rs
new file mode 100644
index 0000000..7c6fea3
--- /dev/null
+++ b/rust/guessing_game/src/main.rs
@@ -0,0 +1,36 @@
+use rand::Rng;
+use std::cmp::Ordering;
+use std::io;
+
+fn main() {
+ 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) // Returns a 'Result' type
+ .expect("Failed to read line");
+
+ // /- Returns a Result type
+ let guess: u32 = match guess.trim().parse() {
+ Ok(num) => num,
+ Err(_) => continue,
+ };
+
+ println!("You guessed: {guess}");
+
+ match guess.cmp(&secret_number) {
+ Ordering::Less => println!("Too small!"),
+ Ordering::Greater => println!("Too big!"),
+ Ordering::Equal => {
+ println!("You win!");
+ break;
+ } // Code block executed if match hits ::Equal
+ };
+ }
+}