Skip to content

Method Call Resolution #1018

Open
Open
@eeff

Description

@eeff

After reading the Method Call Expression section, I am still confused about the method resolution. For example, the following code outputs in inherent impl instead of in trait impl. But according to the reference, shouldn't the type candidate list be &Foo, &&Foo, &mut &Foo, Foo, &Foo, &mut Foo? And so the trait method would be found first? Could somebody shed more light on this ?

trait Trait {
    fn method(self);
}

struct Foo;

impl Foo {
    fn method(&self) {
        println!("in inherent impl");
    }
}

impl Trait for &Foo {
    fn method(self) {
        println!("in trait impl");
    }
}

fn main() {
    let foo = &Foo;
    foo.method();
}

The output is:

$ cargo run
in inherent impl
$ rustc --version
rustc 1.53.0-nightly (f82664191 2021-03-21)

Found a related StackOverflow, but I still don't understand why this is so.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions