[Avg. reading time: 3 minutes]
Reference and Deference operators
In Rust, values live in memory. Sometimes you want to use a value without copying it. That is where references come in.
& - creates a reference
* - accessess the value through a reference
// Reference and Dereference fn main() { let a = 10; // a owns the value 10 let b = &a; // b is a reference to a // value of a and address of a println!("a value: {} | a address: {:p}", a, &a); // dereferenced value from b and address stored in b println!("b value: {} | b address: {:p}", *b, b); // same value and same address, written explicitly println!("value via & then *: {} | address via * then &: {:p}", *(&a), &(*b)); }
For Example

Why its useful?
- Avoids unnecessary copying of data.
- Allows functions to read large values efficiently.
- Makes ownership rules explicit. (Will discuss this in next chapter)