What is Borrowing?
Wait! If every function took ownership of its arguments, coding would be a nightmare. Borrowing allows you to pass a reference to a value instead of the value itself.
We use the
& symbol to create a reference.rust
Mutable References (&mut)
By default, references are immutable. You cannot change what you have borrowed. If you want to modify the value, you must use a mutable reference.
rust
The Golden Rules of Borrowing
Rust’s Borrow Checker enforces two major rules to prevent "Data Races":
- You can have any number of immutable references (
&T) OR - You can have exactly one mutable reference (
&mut T). - You cannot have both at the same time.
rust
Dangling References
Rust prevents "Dangling Pointers"—references that point to memory that has been deleted. The compiler will stop you if you try to return a reference to a local variable.
rust
Summary
| Term | Syntax | Description |
|---|---|---|
| Reference | &T | Borrowing without ownership |
| Mut Ref | &mut T | Borrowing with modification permission |
| Dereference | *r | Accessing the value behind the reference |
| Safety | Prevents data races at compile time | |
| Borrow C. | The part of the compiler that enforces these rules | |
| Exclusive | Mutable references are "Exclusive" | |
| Shared | Immutable references are "Shared" | |
| Scope | A reference's lifetime ends at its last usage | |
| Modern | Non-Lexical Lifetimes (NLL) make borrowing easier | |
| Zero-Cost | References are just pointers (no runtime overhead!) | |
| Logic | You can have many readers OR one writer. Never both! | |
| Safety | Prevents "Dangling pointers" and "Use-after-free" |