首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >no #[repr(.)]用于铁锈箱

no #[repr(.)]用于铁锈箱
EN

Stack Overflow用户
提问于 2022-09-15 08:43:38
回答 1查看 66关注 0票数 -1

我试图解决一个关于使用外部板条箱的琐碎而简单的练习任务。

代码语言:javascript
复制
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

此外,在使用包中的方法时:

代码语言:javascript
复制
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)方法时出错

代码语言:javascript
复制
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'

在检查类似的解决方案时,它们的实现方式与我的完全相同。有小费吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-15 09:04:14

这是没有文档化的,但显然#[derive(IntEnum)]要求枚举具有一个#[repr(integer)],对于每个变量都需要一个显式整数值(Name = value),另外还需要the IntEnum trait requires the type to be Copy

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73728112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档