Chapter 2
Programming a Guessing Game
To hands-on learn Blyx concepts, let us build a complete guessing game program that generates a secret random number between 1 and 100 and prompts the user to guess it.
use std::io;
use std::rand;
fn main() {
let secret = rand::thread_rng().gen_range(1..=100);
println!("Guess the secret number!");
loop {
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 == secret {
println!("You win!");
break;
}
}
}