What are the exact rules of auto-dereferencing in Rust?
Hello everyone! I started learning Rust recently and I can't figure out auto-dereferencing. The documentation says that the compiler automatically dereferences references when necessary, but I keep getting confused.
Here's a simple example. I have a `Point` struct with a `distance_from_origin` method. I create an instance, then a reference to it, then a reference to the reference, and try to call the method:
```rust
struct Point {
x: f64,
y: f64,
}
impl Point {
fn distance_from_origin(&self) -> f64 {
(self.x.powi(2) + self.y.powi(2)).sqrt()
}
}
fn main() {
let p = Point { x: 3.0, y: 4.0 };
let r1 = &p; // &Point
let r2 = &&p; // &&Point
let r3 = &&&p; // &&&Point
println!("{}", p.distance_from_origin()
No answers yet.
Log in to leave an answer