#rust book paragraph a bit confusing

7 messages · Page 1 of 1 (latest)

regal yarrow
#

Rust doesn’t have an equivalent to the -> operator; instead, Rust has a feature called automatic referencing and dereferencing. Calling methods is one of the few places in Rust that has this behavior.

Here’s how it works: when you call a method with object.something(), Rust automatically adds in &, &mut, or * so object matches the signature of the method. In other words, the following are the same:


p1.distance(&p2);
(&p1).distance(&p2);

The first one looks much cleaner. This automatic referencing behavior works because methods have a clear receiver—the type of self. Given the receiver and name of a method, Rust can figure out definitively whether the method is reading (&self), mutating (&mut self), or consuming (self). The fact that Rust makes borrowing implicit for method receivers is a big part of making ownership ergonomic in practice.```

it says this but don't C and rust do this for different reasons so they're not really equivelants
vapid python
#

If I had to guess, I'd say it's aimed at people with C/C++ experience, to answer any "wait, that's a pointer. How does . work?" questions

regal yarrow
#

but they're for different reasons right

#

C++ does it so you do it on the object itself and not the pointer

#

and rust does it for other reasons

wicked charm
#

those reasons are pretty similar tho