Memory

// Variable x with value 42
let x = 42;
// Variable y with value 43
let y = 43;
// Variable var1 with value of a pointer to the value of variable x
let var1 = &x;
// Variable var2 with value of a pointer to the value of variable x
// The variable var2 is mutable - changing it's value will 
// not change the value at the place its pointing at.
let mut var2 = &x;
// Point to the value of variable y
// Does not change var1. Even though initially var1 and var2 point 
// to the same place, their values are independent of each other.
var2 = &y;

Place

Values

Pointers

Variables

High-level thought model

// Initially uninitialised
let mut x;
// x does not hold a legal value
// The value at x cannot be borrowed because there is no flow yet which 
// owns the value.
// assert_eq!(x, 42);
// Give x with a value
x = 42;
// A shared flow from the value assigned to x
let y = &x;
// A second exclusive (mutable) flow from the value assigned to x
x = 43;
// Cannot have a shared flow while an exclusive flow exists - y is 
// a shared flow from the value at x. If the following was uncommented, 
// the flow to y would no longer be terminated, so we would have an 
// exclusive and a shared flow existing in parallel, which the borrow
// checker would detect.
// assert_eq!(*y, 42);

// Create a new binding for y - shadows the previous binding
// Create a new shared flow from the value at x
let y = &x;
// The exlusive flow from x terminates where the value to x is mutated. 
// We only have a single shared flow from the value at x. This is legal.
assert_eq!(*y, 43);