Why is there no deref coercion from &String to &str when calling From::from?

Viewed 36

Usually I can use an &String in any function call that accepts an &str, I believe this is because String implements Deref<Target = str>.

In the following case using From it does not work:

struct Foo;

impl From<&str> for Foo {
    fn from(_: &str) -> Foo {
        unimplemented!()
    }
}

fn main() {
    let s = String::new();
    Foo::from(&s);
}

Playground

Yields:

error[E0277]: the trait bound `Foo: From<&String>` is not satisfied
  --> src/main.rs:11:5
   |
11 |     Foo::from(&s);
   |     ^^^^^^^^^ the trait `From<&String>` is not implemented for `Foo`
   |
   = help: the following implementations were found:
             <Foo as From<&str>>
   = note: required by `from`

Why do I not get the Deref I am used to in this case?

0 Answers
Related