Auto dereferncing

I have 2 separate questions.

Whether Rust always do auto-deferencing ?

Here is my code, where it works well with explicit de-reference and
without explicit de-refernce. Can any one confirm this ?

struct Rectangle {

    x : f64,
    y : f64,

fn main()

    let x1 = Rectangle {x : 10.1, y: 20.2};
     let y1 = &x1;
    //y is a reference variable which points to x

fn print_rect(r1 : &Rectangle)
    println!("Inside function {:?}",*r1);

    //Rust doing auto-dereferncing ?
    println!("Inside function {:?}",r1);

You may want to see Implicit Deref Coercions with Functions and Methods from The Rust Programming Language. It explains the rules around when things are automatically dereferenced.

The println!() macro will (eventually) call the Rectangle's fmt() method (from std::fmt::Debug). Rust automatically dereferences if necessary when invoking method calls, so yes, you are seeing auto-deref here.

For the part within the fn print_rect(), no, it's not a autoderef but impl<T> Debug for &'_ T in stdlib.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.