Method resolution doesn't find trait implementation on `&Type`

⚓ Rust    📅 2026-02-16    👤 surdeus    👁️ 1      

surdeus

struct Foo;
struct Bar;

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

fn main() {
    let f: Foo = Bar.into(); // <- doesn't compile - I expected it to do `(&Bar).into()` automatically
    let f: Foo = (&Bar).into(); // Requires explicit `&`
}

(Playground)

Implementing Into instead of the From above doesn't make it compile either, as expected.

impl Into<Foo> for &Bar {
    fn into(self) -> Foo {
        Foo
    }
}

Whereas defining an inherent method does make it compile.

impl Bar {
    fn into(&self) -> Foo {
        Foo
    }
}

It's not clear to me why the original should fail based on the method call resolution documentation. Any insights?

1 post - 1 participant

Read full topic

🏷️ Rust_feed