'Rust - Why we don't need borrow A when calling A::func(&self) from A self [closed]

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?



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 isaactfa