# Handling Bad Input

Here's our current code:

{% code title="main.rs" %}

```rust
use std::io;
use std::cmp::Ordering;

fn main() {
    println!("Guess the number!");

    let secret_number = 42;

    println!("The secret number is: {}", secret_number);

    loop {
        println!("Please input your guess.");

        let mut guess = String::new();

        io::stdin().read_line(&mut guess)
            .expect("Failed to read line");

        let guess: u32 = guess.trim().parse()
            .expect("Please type a number!");

        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;
            },
        }
    }
}
```

{% endcode %}

What's the problem now? Well, right now, if you enter something that is non-numeric, you get an error and it stops the program. Let's handle that!

```rust
let guess: u32 = match guess.trim().parse() {
    Ok(num) => num,
    Err(_) => continue,
};
```

Just like we are doing when we compare the guess, we can also use `match` on the results of `parse`, which only has two varients. We use the `_` because we don't care what the content of `Err` is, we just want to continue.

What if we do want to output the content of `Err`? Let's change the code again to print out the message.

```rust
let guess: u32 = match guess.trim().parse() {
              Ok(num) => num,
              Err(msg) => {
                println!("{}", msg);
                continue;
              },
          };
```

Here's what the output looks like now:

![](https://2720625490-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBlB5eCVilPmyCc9hb0%2F-LBqORxHTP4QVIYGI_-f%2F-LBqOxXkzjHe6XcMaeey%2FScreen%20Shot%202018-05-06%20at%201.02.20%20PM.png?alt=media\&token=30822fe6-4336-4824-90a4-4d56fe0eb325)
