Maciej Goszczycki
Maciej Goszczycki

Reputation: 1128

How are string io::Errors created

Rust docs show a std::io::Error being created using

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

and new has a type signature of

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

I've found an implementation of impl<T: std::error::Error> std::error::Error for Box<T> but cannot find one for &'static str like the one used in the example.

What trait is used to achieve Into<Error> for strings?

Upvotes: 3

Views: 570

Answers (1)

DK.
DK.

Reputation: 59095

Given:

  • impl<'a> From<&'a str> for Box<Error> (see the From docs)
  • impl<T, U> Into<U> for T where U: From<T> (see the Into docs)

Substitute T = &'a str and U = Box<Error>, which gives you:

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

Upvotes: 5

Related Questions