1. What is Borrowing?
Borrowing means accessing a value without taking ownership of it.
Instead of transferring ownership, we pass a reference to the value.
Example Without Borrowing
Here, ownership moves from main() to display_name().
Example With Borrowing
Output:
Here:
nameowns the original string.&namecreates a reference to the string.- The function borrows the value.
- Ownership remains with the original variable.
Important: Borrowing allows us to use a value without taking ownership of it.
2. What is a Reference?
A reference is a way to refer to an existing value without owning it.
In Rust, we use the & symbol to create a reference.
Syntax
Example
Output:
Both print the same value.
However:
numberis the original value.referencerefers to that value.
3. Immutable References
An immutable reference allows us to read a value but not modify it through that reference.
Example
Output:
Here, reference can access the string, but cannot modify it.
Multiple Immutable References
Rust allows multiple immutable references to the same value at the same time.
Example:
Output:
This is allowed because all three references only read the value.
4. Mutable References
A mutable reference allows us to modify the original value.
We use &mut to create a mutable reference.
Syntax
Example
Output:
Here:
nameis declared usingmut.&mut namecreates a mutable reference.push_str()modifies the original string through the reference.
Important
To create a mutable reference, the original variable must also be mutable.
Incorrect:
This produces a compilation error because name is not mutable.
Correct:
5. Borrowing Rules in Rust
Rust follows important rules to prevent data races and unsafe memory access.
Rule 1: Multiple Immutable References Are Allowed
This is allowed.
Rule 2: Only One Active Mutable Reference at a Time
This is allowed.
But creating two simultaneously usable mutable references to the same value is not allowed.
Incorrect:
This produces a compilation error because two mutable references overlap.
Rule 3: Mutable and Immutable References Cannot Overlap
Rust does not allow an immutable reference and a mutable reference to the same value to be used at the same time.
Incorrect:
This causes a compilation error because the references overlap.
Note: Rust checks when references are actually used. Non-lexical lifetimes allow a borrow to end after its last use, even if the variable remains in scope.
6. Understanding Dereferencing (*)
The * operator is called the dereference operator.
It allows us to access the value behind a reference.
Example
Output:
Both print the same value, but they represent different operations.
referenceis a reference.*referenceaccesses the value it refers to.
Modifying Through a Mutable Reference
Output:
Here, *reference = 50 modifies the original value.
7. References in Functions
References are commonly used as function parameters.
This allows functions to use values without taking ownership.
Example
Output:
Here:
&Stringmeans the function accepts a reference to aString.usizeis the return type of.len().- Ownership remains with
main().
Using &str Instead of &String
For functions that only need to read text, &str is often more flexible.
Output:
The same function accepts both a borrowed String and a string slice.
8. Mutable References in Functions
A function can modify a value through a mutable reference.
Example
Output:
Here:
&mut i32means the function accepts a mutable reference to an integer.*salaryaccesses the original integer.- The function modifies the original value without taking ownership.
9. What is a Dangling Reference?
A dangling reference is a reference that points to memory that is no longer valid.
Rust prevents references from outliving the values they refer to.
Incorrect Example
This code produces a compilation error.
Why?
numberis declared inside the inner block.- When the block ends,
numbergoes out of scope. referencewould point to a value that no longer exists.
Rust prevents this problem at compile time.
10. Difference Between Ownership and Borrowing
Feature | Ownership | Borrowing |
|---|---|---|
Meaning | Controls who owns a value | Allows access without ownership transfer |
Symbol | No special symbol |
|
Ownership transfer | Can transfer ownership | Does not transfer ownership |
Modification | Owner can modify if mutable | Mutable reference can modify |
Example |
|
|
11. Quick Reference
Concept | Syntax | Purpose |
|---|---|---|
Immutable reference |
| Read a value |
Mutable reference |
| Modify a value |
Dereference |
| Access the referenced value |
Function borrowing |
| Accept a borrowed String |
Mutable function parameter |
| Modify a borrowed integer |
String slice parameter |
| Accept borrowed text |