Shared and Unique Borrows

Rust puts constraints on the ways you can borrow values:

  • You can have one or more &T values at any given time, or
  • You can have exactly one &mut T value.
  1. fn main() {
  2. let mut a: i32 = 10;
  3. let b: &i32 = &a;
  4. {
  5. let c: &mut i32 = &mut a;
  6. *c = 20;
  7. }
  8. println!("a: {a}");
  9. println!("b: {b}");
  10. }
  • The above code does not compile because a is borrowed as mutable (through c) and as immutable (through b) at the same time.
  • Move the println! statement for b before the scope that introduces c to make the code compile.
  • After that change, the compiler realizes that b is only ever used before the new mutable borrow of a through c. This is a feature of the borrow checker called “non-lexical lifetimes”.