Rust Fundamentals interview question · Most asked
What are Rust's borrowing rules and what do they prevent?
Summary
At any time, either any number of shared references (&T) or exactly one mutable reference (&mut T), and references must never outlive their data. This prevents data races, iterator invalidation and dangling pointers at compile time.
Longer Answer
The borrow checker enforces these rules statically. Non-lexical lifetimes mean a borrow ends after its last use, not at the end of the block.
Key Points
- Many readers XOR one writer.
- No dangling references.
- Checked at compile time.
Deep dive: Rust Fundamentals: Borrowing