This is what I’ve found:
In C and C++, two different operators are used for calling methods: you use . if you’re calling a method on the object directly and -> if you’re calling the method on a pointer to the object and need to dereference the pointer first. In other words, if object is a pointer, object->something() is similar to (*object).something().
What about Rust?
Rust doesn’t have an equivalent to the -> operator. Instead, Rust has what is called automatic referencing and dereferencing.
In other words, the following are the same:
p1.distance(&p2); (&p1).distance(&p2); Note: 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).
I am not sureI understood the note correctly, what does it mean that there is a clear receiver? Also, doesn’t Rust actually have an operator for dereferencing (*) as well?
If you want polymorphism which looks more like what you’re describing, you can put trait bounds on parameters instead of a type and it will accept any parameter that implement those traits. E.g. If you want to accept anything that can be turned into an owned string with “.into()” you type an argument with “impl Into<String>”. Another common one is “impl AsRef<Path>” to accept a path, path reference, PathBuf etc.