Raw data, clear context.

[
[
[

]
]
]

A counter needs two updates through borrowed access. Start at four, let each reference add one, then read the original variable again.

A mutable reference, written &mut value, permits changes to the borrowed value. The original binding must also allow mutation with let mut.[1]

The * operator follows a reference to its value.[2]

Here, *first += 1 updates the counter reached through first. The same spelling works for second.

Create the project below, open the generated borrowed-counter directory, and replace src/main.rs with the complete programme.

Shell
cargo new --vcs none borrowed-counter
Output
$ cargo new --vcs none borrowed-counter
Creating binary (application) `borrowed-counter` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
exit=0
src/main.rs
Rust
fn main() {
let mut count = 4;
let first = &mut count;
*first += 1;
println!("First: {first}");
let second = &mut count;
*second += 1;
println!("Second: {second}");
println!("Owner: {count}");
}

Run it from that project directory:

Shell
cargo run --quiet
Output
$ cargo run --quiet
First: 5
Second: 6
Owner: 6
exit=0

Both updates reach the same counter. The final line reads six through count, after the references have finished being used. These results were captured with Rust 1.99.1 and Cargo 1.99.1 on aarch64 Linux.

For these ordinary borrows, the compiler can end a borrow after the reference’s last use. A closing brace is unnecessary between the two borrows.[1]

The first print is the last use of first. Only afterwards does the programme create second; its last use is the second print. Keep that order in view when reading the error below.

Two timelines for the same counter: sequential uses of first then second compile; creating second before the later uses of first produces E0499.
The working programme finishes using first before borrowing count through second. The failing version starts the second borrow while first is still needed.

Make the borrows overlap

Replace src/main.rs with this deliberately failing version. It moves the creation of second ahead of the final uses of first.

src/main.rs
Rust
fn main() {
let mut count = 4;
let first = &mut count;
let second = &mut count;
*first += 1;
println!("First: {first}");
*second += 1;
println!("Second: {second}");
println!("Owner: {count}");
}
Shell
cargo check --quiet
Output
$ cargo check --quiet
error[E0499]: cannot borrow `count` as mutable more than once at a time
--> src/main.rs:4:18
|
3 | let first = &mut count;
| ---------- first mutable borrow occurs here
4 | let second = &mut count;
| ^^^^^^^^^^ second mutable borrow occurs here
5 | *first += 1;
| ----------- first borrow later used here
For more information about this error, try `rustc --explain E0499`.
error: could not compile `borrowed-counter` (bin "borrowed-counter") due to 1 previous error
exit=101

E0499 identifies multiple mutable borrows of the same value.[3]

Read the diagnostic’s three marked locations: the first borrow, the competing borrow, then the later use that keeps the first borrow needed.

Restore the working version by moving the creation of second below the first print. That restored programme ran successfully.

Shared access also constrains mutation: taking a mutable borrow while a shared borrow of the same value is still needed causes E0502.[4]

Try it

In the working programme, change the second increment to add three. Predict both later lines before running it. Then move the second borrow earlier and locate the conflict yourself.

Next: working with text, including String, &str and Unicode.

Browse Rust, one step at a time

Sources

  1. The Rust Book: References and Borrowing
  2. The Rust Book: Treating Smart Pointers Like Regular References with the Deref Trait
  3. Rust error E0499: multiple mutable borrows
  4. Rust error E0502: incompatible borrows

Leave a Reply