1

See the code:

struct A {}

impl A {
    fn a(&self) {}
}

pub fn main() {
    let a = A {};
    a.a();
    A::a(&a);
}

Why a.a() doesn't need the & while A::a(&a) needs? What's the difference?

doki
  • 85
  • 5
  • 2
    The [dot operator](https://doc.rust-lang.org/nomicon/dot-operator.html) is literally syntax sugar for `A::a(&a)`. – isaactfa May 07 '22 at 11:16
  • 1
    @isaactfa, looks like an answer to me. – hkBst May 07 '22 at 17:13
  • Does this answer your question? [What are Rust's exact auto-dereferencing rules?](https://stackoverflow.com/questions/28519997/what-are-rusts-exact-auto-dereferencing-rules) – Chayim Friedman May 08 '22 at 00:51

1 Answers1

0

In Rust, a.a() is syntax sugar for A::a(&a), so a does get borrowed in both calls. The dot operator does a lot more as well, you can read about that here.

isaactfa
  • 5,461
  • 1
  • 10
  • 24
  • 1
    Also worth mentioning that, in the more general case, if `fn a(&self)` had a receiver of `&mut self` / `self`, the syntactic sugar would be `A::a(&mut a)` / `A::a(a)`, respectively – Filipe Rodrigues May 07 '22 at 23:53