primitive-type
Rust reference: borrow access to a value
A reference gives safe borrowed access without taking ownership of the referenced value. Shared references generally permit reading; mutable references provide exclusive access for mutation. A reference must remain valid for its use. Expected output (not executed): 6 Why: The mutable reference changes stock; its borrow ends before the final direct access. Common mistake: Do not keep competing mutable and shared accesses alive in conflicting ways.
Formal Syntax & Structure
rust syntaxrust
&T; &mut TWorking Example
Example Snippetrust
fn main() { let mut stock = 4; let view = &mut stock; *view += 2; println!("{stock}");}