Dereference-Rust-2018

I have a code here.
y is the owner for the reference of x.
But in Rust 2018, I could compile and run the code sucessfully without
de-referencing. Could someone explain ?.

fn main()
{

    let x = 5;

    let y = &x;

    println!("{}", x);
    println!("{}", y);

}

Formatting with "{}" uses the Display trait. This trait is implemented for references:

impl<T> Display for &T where T: Display + ?Sized

The implementation of Display for references just prints out the value that the reference points to. So printing either 5 or &5 will result in the same output.

3 Likes

Thanks for giving clarifications.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.