Skip to content

Idiomatic: Simplify example in method resolution slide - #3166

Open
randomPoison wants to merge 2 commits into
google:mainfrom
randomPoison:method-resolution-slide-simplify
Open

Idiomatic: Simplify example in method resolution slide#3166
randomPoison wants to merge 2 commits into
google:mainfrom
randomPoison:method-resolution-slide-simplify

Conversation

@randomPoison

Copy link
Copy Markdown
Contributor

The example in the speaker notes can be a bit simpler, i.e. we can do & instead of &mut and get the same effect. I also cut the comment down to reduce verbosity.

@mgeisler
mgeisler enabled auto-merge (squash) April 20, 2026 21:46
`&mut self` has a higher priority than `&self`, the one used by the inherent
method.
Demonstrate: Change the call to `(&-1i32).count_ones()` and demonstrate that
the trait method is now called instead.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that this new example demonstrates the rule described above. Could you help me understand? The difference is the method receiver expression, value -1i32 vs. reference &-1i32. The list above that describes the rule does not talk about that axis?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah good point, the explanation here is wrong too (or at least not fully accurate). After staring at the reference for an hour (and then begging Codex to explain it again but slower), it looks like the reason the & changes resolution is because method resolution searches based on the receiver type, and adding the & changes the order in which it searches the receivers.

When resolving a method, rustc builds a list of candidate receiver types by repeatedly dereferencing and then adding & and &mut. It then searches for methods (both inherent and trait ones) that have the candidate receiver type. When we do (-1i32).count_ones() the candidate list is:

i32 -> &i32 -> &mut i32

When we do (&-1i32).count_ones(), the candidate list is:

&i32 -> &&i32 -> &mut &i32 -> i32 -> &i32 -> &mut i32

In the latter case, it looks for methods that take &i32 before it looks for methods that take i32, so it finds the trait method first. The explanation above is somewhat accurate in that in some cases it will search for &self methods before &mut self methods in some cases, but that's not always the case if a deref is involved.


All that being said, I don't think we should go into the details of method resolution here. I think the main thing to point out is "method resolution mostly Just Works, but it can be subtle and hard to reason about if trait methods overlap inherent methods, so try to avoid that." I'll update the speaker notes to make this more concise and accurate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I've reworked the speaker notes to be more concise, and to more directly frame the discussion of method resolution as "method resolution can be confusing when inherent methods and trait methods overlap, so avoid overlap if possible", and fleshed out the suggested demonstration to illustrate that subtlety.

auto-merge was automatically disabled July 31, 2026 19:38

Head branch was pushed to by a user without write access

@randomPoison
randomPoison requested a review from gribozavr July 31, 2026 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants