This commit is contained in:
Tyrel Souza 2023-02-25 00:44:38 -05:00
parent 53a2181a80
commit 76e0c04081
No known key found for this signature in database
GPG Key ID: F3614B02ACBE438E

View File

@ -1,21 +1,33 @@
use std::io;
use rand::Rng; use rand::Rng;
use std::cmp::Ordering;
use std::io;
fn main() { fn main() {
println!("Guess the number!"); println!("Guess the number!");
let secret_number = rand::thread_rng().gen_range(1..=100); let secret_number = rand::thread_rng().gen_range(1..=100);
println!("psst. the secret number is {secret_number}"); println!("psst. the secret number is {secret_number}");
loop {
println!("Please input your guess."); println!("Please input your guess.");
let mut guess = String::new(); let mut guess = String::new();
io::stdin() io::stdin()
.read_line(&mut guess) .read_line(&mut guess)
.expect("Failed to read line"); .expect("Failed to read line");
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
println!("You guessed {guess}"); 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;
}
}
}
} }