我正在尝试创建实现to_string()
的Error
枚举。我试图为他们derive(Debug)
,但这似乎还不够。
这是我正在处理的枚举:
#[derive(Debug, Clone)]
pub enum InnerError {
InnerErrorWithDescription(String),
}
#[derive(Debug, Clone)]
pub enum OuterError {
OuterErrorWithDescription(String),
}
我想做的是:
// result type <T,InnerErrorWithDescription>
result.map_err(|err| { Error::OuterErrorWithDescription(err.to_string())}) // .to_string() is not available
我无法设法将InnerError
枚举类型转换为OuterError
.
我应该更改什么才能实现它?
我在这里做了一个编写枚举类型及其值的示例:
锈游乐场
但是,我仍然必须在匹配的情况下指定类型和描述,还有更通用的实现吗?
你的枚举应该实现Display
; 来自ToString
文档:
此特征会自动实现为任何实现的类型
Display
特征。因此,不应实现ToString
直接:应该实现Display
,你会得到ToString
免费实施。
编辑:我已经调整了你的游乐场示例;我想你可能会追求这样的事情。