我试图解决一个关于使用外部板条箱的琐碎而简单的练习任务。
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(),
}
}这给了我一个错误,说明no #[repr(...)] found
此外,在使用包中的方法时:
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(),
}
}我在使用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'在检查类似的解决方案时,它们的实现方式与我的完全相同。有小费吗?
发布于 2022-09-15 09:04:14
这是没有文档化的,但显然#[derive(IntEnum)]要求枚举具有一个#[repr(integer)],对于每个变量都需要一个显式整数值(Name = value),另外还需要the IntEnum trait requires the type to be Copy。
https://stackoverflow.com/questions/73728112
复制相似问题