From 21a351914fe6508d7a6a5f976f4c17834e11b542 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sat, 12 Oct 2024 00:29:01 +0200 Subject: [PATCH] Handle parsing errors --- guessing_game/src/main.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/guessing_game/src/main.rs b/guessing_game/src/main.rs index a58990e..951d636 100644 --- a/guessing_game/src/main.rs +++ b/guessing_game/src/main.rs @@ -16,7 +16,14 @@ fn main() { .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(_) => { + println!("Please type a number!"); + continue; + }, + }; + println!("You guessed: {guess}"); match guess.cmp(&secret_number) {