0

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?

cafce25
  • 15,907
  • 4
  • 25
  • 31
Paal Pedersen
  • 1,070
  • 1
  • 10
  • 13
  • 1
    Match arm checking happens on the type level. The compiler only knows that `gen_range()` returns a `u8`, so from the compiler's point of view not all values are covered. You can add `_ => unreachable!()` to state that no other values are possible. – Sven Marnach Aug 08 '23 at 11:29
  • is it a way to change this -> u8 into a range 1-6 somehow? ->Option<(1,6)>. this actually brings forth another error, that Dice does not implement a roll function. – Paal Pedersen Aug 08 '23 at 11:30
  • 5
    Related: [How do I choose a random value from an enum?](https://stackoverflow.com/questions/48490049/how-do-i-choose-a-random-value-from-an-enum) – Sven Marnach Aug 08 '23 at 11:35
  • See also [this answer](https://stackoverflow.com/questions/28028854/how-do-i-match-enum-values-with-an-integer). – tadman Aug 08 '23 at 15:00

1 Answers1

1

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,
            _ => unreachable!(), // <--- Thanks Sven Marnach

        }
    }
}```
Paal Pedersen
  • 1,070
  • 1
  • 10
  • 13
  • Code only answers are discouraged! Please add some text to explain why this is necessary and how it solves OPs problem. – cafce25 Aug 09 '23 at 01:05