From 76e0c04081a651d68fbd3e8e424e434eb8eece2f Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Sat, 25 Feb 2023 00:44:38 -0500 Subject: [PATCH] looping --- guessing_game/src/main.rs | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/guessing_game/src/main.rs b/guessing_game/src/main.rs index c367dd2..5ac4c7f 100644 --- a/guessing_game/src/main.rs +++ b/guessing_game/src/main.rs @@ -1,21 +1,33 @@ -use std::io; 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); - println!("psst. the secret number is {secret_number}"); + loop { + println!("Please input your guess."); + let mut guess = String::new(); + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); - println!("Please input your guess."); + let guess: u32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => continue, + }; - let mut guess = String::new(); - io::stdin() - .read_line(&mut guess) - .expect("Failed to read line"); + println!("You guessed {guess}"); - println!("You guessed {guess}"); - -} \ No newline at end of file + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too Small"), + Ordering::Greater => println!("too big"), + Ordering::Equal => { + println!("you win"); + break; + } + } + } +}