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

How are string io::Errors created

Rust docs 显示 std::io::Error 使用

创建
let custom_error = Error::new(ErrorKind::Other, "oh no!");

new有一个type signature

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

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

什么特征用于实现字符串的Into<Error>

鉴于:

  • 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