I am just playing around with some rust to see if I can manage to learn it. I am just dipping into enums, and impl methods on them. Well I just wanted to try to make a silly dice.
use rand::Rng;
fn main() {
let dice: Dice = Dice::One;
dice.roll();
}
enum Dice {
One,
Two,
Three,
Four,
Five,
Six,
}
impl Dice {
fn _rnd(&self) -> u8 {
rand::thread_rng().gen_range(1..=6)
}
// Mutate self into a new dice side
fn roll(self) -> Dice {
match self._rnd() {
1 => self::Dice::One,
2 => self::Dice::Two,
3 => self::Dice::Three,
4 => self::Dice::Four,
5 => self::Dice::Five,
6 => self::Dice::Six,
}
}
}
Error:
match self._rnd() {
| ^^^^^^^^^^^ patterns `0_u8` and `7_u8..=u8::MAX` not covered
|
= note: the matched value is of type `u8`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
|
93 ~ 6 => self::Dice::Six,
94 ~ 0_u8 | 7_u8..=u8::MAX => todo!(),
Well the compiler thinks that I have not exausted all the posibillities of self._rnd which actually only returns a number between 1 and 6. How can I make the "match" understand this without making too much noice?