字符串 io::Errors 是如何创建的

Mac*_*cki 3 error-handling std traits rust

Rust文档显示了std::io::Error使用创建的

let custom_error = Error::new(ErrorKind::Other, "oh no!");
Run Code Online (Sandbox Code Playgroud)

并且new有一个类型签名

fn new<E>(kind: ErrorKind, error: E) -> Error 
  where E: Into<Box<std::error::Error + Send + Sync>>
Run Code Online (Sandbox Code Playgroud)

我找到了一种实现impl<T: std::error::Error> std::error::Error for Box<T>但找不到&'static str像示例中使用的那样的实现。

使用什么特征来实现Into<Error>字符串?

DK.*_*DK. 5

鉴于:

  • impl<'a> From<&'a str> for Box<Error>(参见From文档)
  • impl<T, U> Into<U> for T where U: From<T>(参见Into文档)

替换T = &'a strU = Box<Error>,得到:

  • impl<'a> Into<Box<Error>> for &'a str