loop structure.

This commit is contained in:
licsber 2024-08-21 17:08:27 +08:00
parent 31a6753312
commit 35796d912b

View File

@ -7,21 +7,26 @@ fn main() {
println!("Secret number is {:?}", secret_number);
println!("Guessing a number:");
loop {
println!("Guessing a number:");
let mut guess_number = String::new();
let mut guess_number = String::new();
io::stdin().read_line(&mut guess_number)
.expect("Failed to read line.");
io::stdin().read_line(&mut guess_number)
.expect("Failed to read line.");
let guess_number: u32 = guess_number.trim().parse()
.expect("Not a number.");
let guess_number: u32 = guess_number.trim().parse()
.expect("Not a number.");
println!("You guess : {:?}", guess_number);
println!("You guess : {:?}", guess_number);
match guess_number.cmp(&secret_number) {
Ordering::Less => println!("Too small."),
Ordering::Greater => println!("Too big."),
Ordering::Equal => println!("You win!")
match guess_number.cmp(&secret_number) {
Ordering::Less => println!("Too small."),
Ordering::Greater => println!("Too big."),
Ordering::Equal => {
println!("You win!");
break;
}
}
}
}