rust-lang / rust-lang/reference
Method Call Resolution
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 1.6k
- Forks
- 607
- PR merge metrics
- PR metrics pending
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Method Call Expression section linked in the report and reproduce the supplied example with the stated rustc version. Clarify the candidate-list and inherent-versus-trait lookup behavior in that reference section; done when the example's output and resolution order are explained consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100