Can I avoid `_` in pattern matching with gen_range?

Viewed 153

If I want to match the result of a rand::thread_rng().get_range(1, 3), I need to add a _ value even if I know there is only two possible values:

match rand::thread_rng().gen_range(1, 3) {
    1 => println!("1"),
    2 => println!("2"),
    _ => panic!("never happens")
};

The _ case is useless but required.

I understand the compiler can't guess that gen_range(1, 3) can only return 1 or 2 but is there a way to avoid adding this useless line _ => panic!("never happens") with pattern matching (maybe with some hint to the compiler)? Or do I need to replace the last value (2) by _?

1 Answers
Related