← Rust Programming learning pathRust Programming · Module 4 of 10
Borrowing and lifetimes
Use references without taking ownership.
📖 Key ideas
- &x borrows immutably; &mut x borrows mutably.
- Many immutable borrows OR one mutable borrow at a time.
- References must never outlive the data (no dangling pointers).
- Lifetimes ('a) describe how long references are valid.
- 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