Function call auto dereference parameters

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(a); // expected `&A`, found struct `A`

This is the same feature as what turns &Vec<T> into &[T] and &String into &str, and it is called Deref coercion.

1 Like

RFC 241, the book, nomicon.

1 Like

I was under the impression Deref Coercion only applies to the structs implementing Deref trait. I guess here it's obvious that &A should derefs to A.

I was more referring to this line "Obtain these by repeatedly dereferencing the receiver expression's type" from Method-call expressions.

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 {

That's what I missed. Thanks.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.