According to the doc Method-call expressions, receiver type is auto dereferenced in method calls. Based on my tests, this is also the case for free function calls.
Curious if there is official doc on this?
fn print_me(a: &A) {
println!("print_me: {}", a.a);
}
struct A { a: i32 }
trait B { fn print_me(&self) {} }
impl B for A { fn print_me(&self) { println!("A::print_me: {}", self.a) } }
fn main() {
let a = A { a: 17i32 };
let b = &a;
print_me(b);
print_me(&b);
print_me(&&b);
print_me(&&&b);
// print_me(a); // expected `&A`, found struct `A`
a.print_me();
}
What you are seeing is deref-coercion. Your link with method-call expressions applies only to self in method calls. Deref-coercion applies because the standard library has the following Deref impl for all references:
impl<T: ?Sized> Deref for &T {
type Target = T;
fn deref(&self) -> &T {
*self
}
}