[SOLVED] Keeping program execution after error


#1

I want to check if user input is valid and I can’t find anything on Google except some library for input validation (and I don’t want to import whole new library just for this);

match input.trim().parse::<usize>() {
    Ok(n) => field = n,
    Err(e) => field = 0,
}

Since field is invalid if it’s zero, reading of the input will happen again later, but the problem is stopped execution of program after Err is returned. Is it possible to keep execution of the program even when Err is returned or I need different approach?

Edit: Looks like I haven’t cleared the input.

match input.trim().parse::<usize>() {
    Ok(n) => field = n,
    Err(e) => {
        input.clear();
        field = 0;
    }
}

#2

BTW there is nothing special about Result at all. As far as Rust is concerned it’s just an enum. Same with Option too or any of the other standard types.


#3

Thanks for the explanation. I have read about that in section about enums in Rust book. It’s pretty clever feature of the language.