In Rust as a high level view you can imagine variables just as names. When a variable is accessed, you can imagine drawing a line from the previous access to the new one. This establishes a dependency relationship, called flow.

Flows can fork and merge, and each split has a distinct lifetime. The compiler checks if the graph is valid, as an example it checks:

  • that two parallel flows do not have mutable access to a value
  • that a flow that borrows a value while there is no flow that owns the value exists

To understand it, see the following code:

let mut x;
x=42;
let y=&x;
x=43;
assert_eq!(*y, 42);

which has two flows: line 2-4 (with one mutable access) and line 3-5 (with a reference). This code fails to compile since it has both an exclusive write (line 4) and an shared borrow (line 5).

See also

References

  • J. Gjengset, Rust for Rustaceans: Idiomatic Programming for Experienced Developers. No Starch Press, 2021.