-1

I'm trying to solve a trivially simple exercism task on using external crates.

use int_enum::IntEnum;

#[derive(Debug, PartialEq, Eq, IntEnum)]

pub enum ResistorColor {
    Black = 0,
    Brown = 1,
    Red = 2,
    Orange = 3,
    Yellow = 4,
    Green = 5,
    Blue = 6,
    Violet = 7,
    Grey = 8,
    White = 9,
}

pub fn value_to_color_string(value: u32) -> String {
    match ResistorColor::from_int(value) {
        Ok(r) => format!("{:?}", r),
        Err(r) => "value out of range".to_string(),
    }
}

which gives me an error stating that no #[repr(...)] found

And further when using methods from the package:

pub fn value_to_color_string(value: u32) -> String {
    match ResistorColor::from_int(value) {
        Ok(r) => format!("{:?}", r),
        Err(r) => "value out of range".to_string(),
    }
}

I get an error when using the method from_int(value)

items from traits can only be used if the trait is implemented and in scope
the following trait defines an item 'from_int', perhaps you need to implement it:
candidate #1: 'IntEnum'

When checking similar solutions, they are implemented in the exact same way as mine. Any tips?

1 Answers1

0

This is not documented, but apparently #[derive(IntEnum)] requires the enum to have a #[repr(integer)], for each variant to have an explicit integer value (Name = value), and in addition the IntEnum trait requires the type to be Copy.

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77