如何将to_string()功能添加到枚举?

Web*_*rix 6 string error-handling traits rust

我正在尝试创建Error实现的枚举to_string().我曾试图derive(Debug)为他们,但似乎还不够.

这是我正在研究的枚举:

#[derive(Debug, Clone)]
pub enum InnerError {
    InnerErrorWithDescription(String),
}

#[derive(Debug, Clone)]
pub enum OuterError {
    OuterErrorWithDescription(String),
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是:

// result type <T,InnerErrorWithDescription>
result.map_err(|err| { Error::OuterErrorWithDescription(err.to_string())}) // .to_string() is not available
Run Code Online (Sandbox Code Playgroud)

我无法将InnerError枚举类型转换为OuterError.

我应该改变什么来实现它?

我在这里写了一个编写枚举类型及其值的例子:

铁锈游乐场

但是,我仍然必须在匹配情况下指定类型和它的描述,是否有更通用的实现?

lje*_*drz 8

你的枚举应该实现Display; 来自ToStringdocs:

对于实现Display特征的任何类型,都会自动实现此特征.因此,ToString不应该直接实现:Display应该实现,而且您可以ToString免费获得 实现.

编辑:我调整了你的游乐场示例; 我想你可能会喜欢这样的事情.