#[Solved] How can I `match` String errors here?

6 messages ยท Page 1 of 1 (latest)

flat knoll
#

Hi! First post here. I like this community ๐Ÿ˜„ .

I'm still learning the basics of Rust. I'm learning std::io::stdin and I arrived here:

fn main() {
    println!("Welcome to");
    println!("Box simulator 1.0");

    let mut response = String::new();

    println!("Write the box number: \n(between 0 and 65 535)");

    stdin().read_line(&mut response).expect("Failed input");

    let tiny_box = match response
        .trim_end() // remove the \r\n
        .parse::<u16>()
    {
        Ok(number) => number,
        Err(error) => match error.kind() {
            IntErrorKind::PosOverflow => panic!("You can't pick a number greater than 65 535"),
            IntErrorKind::NegOverflow => panic!("You can't pick a number less than 0"),
            IntErrorKind::Empty => panic!("I read an empty value"),
            other_error => {
                panic!("Something wrong has happened: {:?}", other_error)
            }
        },
    };

    println!("you the number {:?}", tiny_box)
}

The objective is to make an app to ask if you would like to edit a box (add a string to the contents), print a box, or quit.
the match error.kind only matches number errors, but if I put a letter the compiler panics with:

note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace```
Where can I catch the letter errors? (longer than 1 character, any other character other than `[epq]`
Thanks in advance!
north trail
#

you missed some variants

#

What you probably want to handle is the InvalidDigit variant

flat knoll
#

Thank you! I'lll try and give you feedback if I worked out

flat knoll
#

Thanks! It solved the problem! ๐Ÿ˜„