[Solved] Why do references need to be explicitly dereferenced?

i'd avoid using C++ references to think about Rust references, they are two different things. So I'd treat & types in Rust like you would * types in C/C++, while keeping in mind the following points.

  1. A Rust & type is stored as a pointer, sometimes with a length and other information (see below responses).
  2. When you call foo.bar(), or access foo.bar, rust will automatically dereference foo if it has a type of &Foo.
  3. There is a trait called Deref that some smart pointer types implement, to change how the * operator works.

More details can be found in the book.

1 Like