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