Looping and breaking

This commit is contained in:
Dan Buch 2017-12-21 23:05:33 -05:00
parent 71b5816a4d
commit ee08b35cc8
Signed by: meatballhat
GPG Key ID: 9685130D8B763EA7

View File

@ -11,21 +11,29 @@ fn main() {
println!("The secret number is: {}", secret_number);
println!("Please input your guess.");
loop {
println!("Please input your guess.");
let mut guess = String::new();
let mut guess = String::new();
io::stdin().read_line(&mut guess)
.expect("Failed to read line");
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
let guess: u32 = guess.trim().parse()
.expect("Please type a number!");
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!"),
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
}
}
}
}