In this lesson, we explore Rust’s borrowing rules using references, illustrating how to safely use mutable and immutable references. Rust enforces a strict rule: you can have either one mutable reference or any number of immutable references at any given time. Once data is moved or goes out of scope, referencing it is not allowed. The examples below demonstrate how to combine mutable and immutable references while adhering to Rust’s safety constraints.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Combining Immutable and Mutable References in Different Scopes
In the first example, two immutable references are created within an inner block and used for reading data. After the inner block ends, these references go out of scope, allowing the creation of a mutable reference that modifies the data.
Rust allows multiple immutable references because they only read data without modifying it. After they go out of scope, a mutable reference can safely modify the original data.
Creating Immutable References Followed by a Mutable Reference
This example shows immutable references being used in a print statement. Once their purpose is fulfilled, a mutable reference is then created within the same outer block. Rust detects that the immutable references are no longer needed and permits the mutable borrow.Conflict Between Immutable and Mutable References
In this example, a mutable reference is attempted while immutable references are still in scope. The immutable references are reused after trying to borrow mutably, leading to a compile-time error.This error occurs because r1 and r2 remain in scope when the mutable reference r3 is created. Ensure that immutable references are not used after a mutable borrow to maintain memory safety.
rustc --explain E0502.
Key Borrowing Rules in Rust
Rust’s strict borrowing rules are designed to ensure data consistency and memory safety. The fundamentals include:- Multiple Immutable References: Numerous immutable references are allowed as long as they only read data, preventing data races.
- Single Mutable Reference: Only one mutable reference is allowed at a time to avoid concurrent data modifications.
- No Overlap: Immutable and mutable references cannot coexist. Once immutable references exist, you cannot create a mutable reference until they fall out of scope, and vice versa.

Example: Immutable References
Consider the following scenario where immutable references share the same data:Example: Mutable Reference
Using a mutable reference is equally simple, with the key restriction of only one mutable borrow at a time:In summary, Rust’s reference rules ensure safe and consistent data management by allowing multiple immutable references for reading and a single mutable reference for writing. By following these guidelines, you can write efficient and memory-safe Rust code. For more on Rust’s borrowing system, check out the Rust Documentation.