← Rust Programming learning path
Rust Programming · Module 4 of 10

Borrowing and lifetimes

Use references without taking ownership.

📖 Key ideas

  1. &x borrows immutably; &mut x borrows mutably.
  2. Many immutable borrows OR one mutable borrow at a time.
  3. References must never outlive the data (no dangling pointers).
  4. Lifetimes ('a) describe how long references are valid.
  5. Most lifetimes are inferred (elision).
💡 Real-life example: Borrowing is lending your notebook: many can read it, but only one can write in it at a time.
ReferenceBorrow of a value
&mutMutable borrow
LifetimeScope a reference is valid for
Dangling referencePointer to freed memory