2017-02-01 3 views
1

Rust docs montrent un std::io::Error être créé à l'aideComment sont chaîne io :: Les erreurs créées

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

et new a une type signature de

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

J'ai trouvé un implementation de impl<T: std::error::Error> std::error::Error for Box<T> mais ne trouve pas un pour &'static str comme celui utilisé dans l'exemple.

Quel trait est utilisé pour atteindre Into<Error> pour les chaînes?

Répondre

3

Vu:

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

de remplacement T = &'a str et U = Box<Error>, ce qui vous donne:

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