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.
cargo new --vcs none borrowed-counter
$ cargo new --vcs none borrowed-counter Creating binary (application) `borrowed-counter` packagenote: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.htmlexit=0
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:
cargo run --quiet
$ cargo run --quietFirst: 5Second: 6Owner: 6exit=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.

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.
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}");}
cargo check --quiet
$ cargo check --quieterror[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 here4 | let second = &mut count; | ^^^^^^^^^^ second mutable borrow occurs here5 | *first += 1; | ----------- first borrow later used hereFor more information about this error, try `rustc --explain E0499`.error: could not compile `borrowed-counter` (bin "borrowed-counter") due to 1 previous errorexit=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
Leave a Reply