skip to content
Alan Kang

Use of & and * in Rust

/ 1 min read

Last Updated:

We will be looking at how variables are declared and referenced/dereferenced in rust.


This variable is immutable and referenced variable cannot change the value of y

let x: u64 = 10

This variable is mutable and referenced variable can change the value of y

let mut y: u64 = 10

This variable has an immutable reference to x therefore, it cannot be dereferenced to change its value

let z: &u64 = &x

This variable has a mutable reference to y and can change the value of y

let b: &u64 = &mut y

You can also make immutable references to mutable variables

let n: &u64 = &y

The following variable makes immutable references to y therefore, intentionally declaring an immutable variable that references a mutable variable, y.