Rust

Fix Rust E0499 "cannot borrow as mutable more than once at a time" — Two Bindings, a Function Call, Two Vec Elements, and a Method Call

One value, two live mutable borrows, and the compiler stops the build. This guide separates the four shapes that produce the error — two bindings, a function argument, two indexed elements, and a method call — and matches each one to ending the first borrow, reborrowing, splitting the data, or working through one borrow.

Written November 22, 2026 · Tested with Rust 1.99.0 · 16 min read

By the end of this guide
  • The rule underneath: one mutable borrow at a time
  • How to read the diagnostic
  • The first failure: two mutable bindings to one variable
  • The second failure: a function call while a borrow is still in use

Written November 22, 2026

Tested with Rust 1.99.0

What this guide is

Error E0499 is the second mutable borrow. The error codes index defines it in one line: a variable was borrowed as mutable more than once. The rule behind it comes from the Rust Book chapter on references and borrowing: if you have a mutable reference to a value, you can have no other references to that value. Shared references may multiply freely, because readers cannot change the data under each other. A mutable reference is the opposite deal — it exists so its holder can change the value, and that promise only holds if it is the only reference in play.

The four forms in this guide cover most E0499 reports in ordinary code. Two &mut bindings to the same variable, both used afterward. A &mut held in a variable while a function call creates another &mut of the same value. Two &mut borrows into one Vec, taken by indexing two different elements. And a method call on a value while a &mut to it is still needed later. In each family the failing program is small, the full compiler output is shown, and the fix keeps the intent of the code while giving the value one mutable borrow at a time.

Build every program in this guide with rustc --edition 2021. In the compiler transcripts below, the file names are the bare names shown on the command line.

The rule underneath: one mutable borrow at a time

A reference's scope starts where it is introduced and continues through its last use — the Book states this directly, and it is the fact that decides most E0499 cases. A borrow that is never used again is already over, even if the variable holding it is still in scope in the textual sense. A borrow that is used on a later line is alive across everything between its creation and that last use, and any second mutable borrow created inside that span is the error.

That is why the same two lines can fail or pass depending on what follows them. Create r1, use it, then create r2 and the code is fine: the borrows are sequential, not simultaneous. Create r1, create r2, then use r1 and the code fails, because the later use stretches the first borrow across the second one's creation. The diagnostic marks all three points, so it can be read as a small timeline rather than as a verdict on any single line.

The Book gives the reason for the restriction in the same section: preventing two live mutable references prevents data races at compile time. Two writers, or a writer and a reader, working on the same data with no synchronization is the shape the rule excludes — and it excludes it by counting borrows, not by inspecting what the code does with them. Two borrows into provably different elements of one vector still count as two borrows of the vector, which is why the third family below needs a split rather than an argument about indices.

How to read the diagnostic

Three labels appear in every E0499 diagnostic, and they name the timeline. "first mutable borrow occurs here" marks the borrow that was already alive. "second mutable borrow occurs here" marks the line the compiler rejected — this is the line in the error[E0499] header's position, and usually the line to change. "first borrow later used here" marks the use that keeps the first borrow alive across the second one. If that third label is missing, the first borrow was not the problem the labels suggest, and the span to look at is the block or function the borrows sit in.

The fix follows from which end of the timeline is flexible. If the later use can move earlier, or the second borrow can move later, the borrows stop overlapping and no restructuring is needed. If both uses genuinely need to be live at once, the code is asking for two writers to one value, and the repair has to change the data layout — split it into disjoint parts — or change who holds the borrow, by passing the existing borrow along instead of minting a new one.

The first failure: two mutable bindings to one variable

This is the shape from the error codes index itself, with both borrows used after the second one is created. Each binding looks harmless on its own; the failure is in the combination, because the use of r1 after r2 exists is what makes the two borrows simultaneous:

Rust
fn main() {
    let mut s = String::from("hello");
    let r1 = &mut s;
    let r2 = &mut s;
    r1.push_str(" one");
    r2.push_str(" two");
    println!("{s}");
}
Terminal
$ rustc --edition 2021 fail_two.rs -o fail_two
error[E0499]: cannot borrow `s` as mutable more than once at a time
 --> fail_two.rs:4:14
  |
3 |     let r1 = &mut s;
  |              ------ first mutable borrow occurs here
4 |     let r2 = &mut s;
  |              ^^^^^^ second mutable borrow occurs here
5 |     r1.push_str(" one");
  |     -- first borrow later used here

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0499`.

Nothing in this program needs both borrows at once — the two appends happen one after the other. The cheapest honest fix is to let the first borrow end before the second begins. A block makes the end explicit: r1 is dropped at the closing brace, so the borrow it held is finished before &mut s is taken again:

Rust
fn main() {
    let mut s = String::from("hello");
    {
        let r1 = &mut s;
        r1.push_str(" one");
    }
    let r2 = &mut s;
    r2.push_str(" two");
    println!("{s}");
}
Terminal
$ rustc --edition 2021 fix_two.rs -o fix_two
$ ./fix_two
hello one two

The output shows both appends landed, in order. The block is the explicit form of a rule that also works implicitly: because a reference's scope ends at its last use, moving the r1.push_str line above the creation of r2 would compile without braces. Reach for the block when the borrow does real work across several lines and the boundary deserves to be visible; reach for reordering when the overlap is a single stray use.

The second failure: a function call while a borrow is still in use

Holding a &mut in a variable and then passing &mut of the same value to a function is the same overlap, with the second borrow hidden inside a call argument. The call looks like it borrows the value only briefly — and it does — but brief is still simultaneous when the held borrow is used on the next line:

Rust
fn bump(x: &mut i32) {
    *x += 1;
}

fn main() {
    let mut n = 1;
    let r = &mut n;
    bump(&mut n);
    *r += 10;
    println!("{n}");
}
Terminal
$ rustc --edition 2021 fail_fn.rs -o fail_fn
error[E0499]: cannot borrow `n` as mutable more than once at a time
 --> fail_fn.rs:8:10
  |
7 |     let r = &mut n;
  |             ------ first mutable borrow occurs here
8 |     bump(&mut n);
  |          ^^^^^^ second mutable borrow occurs here
9 |     *r += 10;
  |     -------- first borrow later used here

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0499`.

The code already holds the only borrow it needs. The fix is to lend that borrow to the function instead of creating a second one from the original variable. Writing &mut *r is a reborrow: it creates a short-lived mutable reference derived from r, hands it to bump, and gives r back when the call returns, so r stays usable afterward:

Rust
fn bump(x: &mut i32) {
    *x += 1;
}

fn main() {
    let mut n = 1;
    let r = &mut n;
    bump(&mut *r);
    *r += 10;
    println!("{n}");
}
Terminal
$ rustc --edition 2021 fix_fn.rs -o fix_fn
$ ./fix_fn
12

The output is the sum the original program intended: 1, plus 1 from bump, plus 10 through r. Passing r itself — bump(r) — also compiles in this shape and also leaves r usable afterward, because bump is known to take a &mut i32, so the compiler inserts the same reborrow automatically at the call. The explicit &mut *r form states that reborrow in the source instead of leaving it to inference, which is why this guide writes it that way. What does not work is the failing form above: minting a fresh &mut n from the original variable while r is still needed later.

The third failure: two elements of one Vec by index

Two different indices feel like two different values. To the borrow checker they are two mutable borrows of the same vector: indexing borrows the container, not just the element, and the compiler does not track which index a borrow came from. So the disjointness that is obvious to a reader is invisible in the types:

Rust
fn main() {
    let mut v = vec![1, 2, 3];
    let a = &mut v[0];
    let b = &mut v[2];
    *a += 10;
    *b += 30;
    println!("{v:?}");
}
Terminal
$ rustc --edition 2021 fail_vec.rs -o fail_vec
error[E0499]: cannot borrow `v` as mutable more than once at a time
 --> fail_vec.rs:4:18
  |
3 |     let a = &mut v[0];
  |                  - first mutable borrow occurs here
4 |     let b = &mut v[2];
  |                  ^ second mutable borrow occurs here
5 |     *a += 10;
  |     -------- first borrow later used here
  |
  = help: use `.split_at_mut(position)` to obtain two mutable non-overlapping sub-slices

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0499`.

The help line names the repair, and it is a real one rather than a guess: the slice documentation describes split_at_mut as dividing one mutable slice into two at an index, the first holding the indices before the split point and the second holding the rest. Because the two results are separate slices with separate borrows, each can be indexed mutably while the other is alive — the split is how disjointness becomes something the types can state:

Rust
fn main() {
    let mut v = vec![1, 2, 3];
    let (left, right) = v.split_at_mut(2);
    let a = &mut left[0];
    let b = &mut right[0];
    *a += 10;
    *b += 30;
    println!("{v:?}");
}
Terminal
$ rustc --edition 2021 fix_vec.rs -o fix_vec
$ ./fix_vec
[11, 2, 33]

Element 0 lives in left and element 2 is element 0 of right, so both updates land and the printed vector shows both. split_at_mut panics if the split index is past the end of the slice, the same contract as indexing itself; the checked sibling returns an Option instead. The same pattern covers two regions of an array or a slice, and split_mut covers the case where the pieces are groups separated by a predicate rather than one split point. When the two elements are adjacent fields of a struct rather than indices of a container, no split is needed at all: field borrows are tracked separately, so &mut h.x and &mut h.y may be live together — it is indexing, which goes through one borrow of the whole container, that loses the distinction.

The fourth failure: a method call on an already-borrowed value

A method that takes &mut self borrows its receiver mutably for the call. That includes the most ordinary calls: push_str on a String borrows the string mutably, so calling it on the original variable while a &mut to that variable is still needed is a second mutable borrow, even though no &mut appears in the call's text:

Rust
fn main() {
    let mut text = String::from("hello");
    let r = &mut text;
    text.push_str(" world");
    r.push_str("!");
    println!("{text}");
}
Terminal
$ rustc --edition 2021 fail_method.rs -o fail_method
error[E0499]: cannot borrow `text` as mutable more than once at a time
 --> fail_method.rs:4:5
  |
3 |     let r = &mut text;
  |             --------- first mutable borrow occurs here
4 |     text.push_str(" world");
  |     ^^^^ second mutable borrow occurs here
5 |     r.push_str("!");
  |     - first borrow later used here

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0499`.

The caret sits under text itself, not under a &mut expression — that placement is the tell for this family. Both appends in this program want the same capability, and the held borrow already provides it. The fix is to do both jobs through the one borrow: call the method on r instead of on text, and let text stay untouched until the borrow ends:

Rust
fn main() {
    let mut text = String::from("hello");
    let r = &mut text;
    r.push_str(" world");
    r.push_str("!");
    println!("{text}");
}
Terminal
$ rustc --edition 2021 fix_method.rs -o fix_method
$ ./fix_method
hello world!

The output is the string both calls were building. This single-borrow form is the right default whenever the second borrow adds no new reach: a method on the original and a method through its &mut can do the same work, so keeping one borrow is simpler than sequencing two. When the call on the original is the one that must stay — because it is a different method the borrow cannot name, or a macro, or a field access the borrow does not cover — the sequencing fixes from the first family apply instead: finish with the borrow, then call.

The fixes in reach order

  • Let the first borrow end when the uses are sequential anyway: reorder so the first borrow's last use comes before the second borrow is created, or wrap the first borrow in a block so its end is explicit.
  • Pass the borrow you hold when a function needs temporary access: reborrow with &mut *r so the caller's borrow survives the call, instead of borrowing the original value a second time.
  • Do both jobs through one borrow when the second borrow adds no reach: call the methods on the &mut itself rather than alternating between the value and its borrow.
  • Split the data when two parts genuinely need simultaneous mutable access: split_at_mut for two regions of a slice, vector, or array, split_mut for predicate-separated groups, and plain field borrows for disjoint struct fields, which need no split.
  • Restructure when the overlap is the design: compute with one borrow and store the result, hand ownership to the code that mutates, or merge the two passes over the data into one pass that holds a single borrow.
  • Use RefCell only when the borrow pattern is correct at run time but cannot be shown to the compiler — shared ownership with mutation through shared references is its case. The RefCell documentation moves the same rule to run time: borrow_mut panics if the value is currently borrowed, so an overlap that was a compile error becomes a panic instead. That trade is worth making deliberately, not as a way to silence the diagnostic.

Traps

  • Deleting the later use silences the error and changes the program. If a "first borrow later used here" label points at a use the code needs, the borrow really is alive there, and the repair is sequencing or splitting — not removing the use to please the checker.
  • Two indices are not two borrows the checker can tell apart. &mut v[0] and &mut v[2] both borrow v as a whole, no matter how far apart the indices are; the same applies to two get_mut calls on one container. Disjointness has to come from a split or from iteration, not from the indices themselves.
  • iter_mut is not a way to hold two element borrows at once either — it yields one element borrow at a time inside a loop body, which is exactly the sequential shape that works. Code that needs to compare or swap neighboring elements uses the split methods, or windows in its shared form for reading.
  • A reborrow ends when the call returns. Storing the reborrow, or a value derived from it, past that point re-creates the overlap under a different name and the error returns at the storing line.
  • This error is not E0502. E0499 is two mutable borrows; E0502 is a mutable borrow overlapping a shared one. The fixes overlap — sequencing helps both — but a fix that converts a borrow to shared form can turn an E0499 report into an E0502 one without solving the underlying overlap.
  • Cloning the value so each borrow has its own copy compiles, and is sometimes the right call for small data. As a reflex it hides the sequencing the code was missing and pays an allocation for it; reach for it after the borrow fixes, not before.

Decision summary

  • The second borrow is a plain &mut binding and the first borrow's work can finish first: reorder the uses, or scope the first borrow in a block.
  • The second borrow is a function argument: reborrow through the held borrow with &mut *r, or finish with the held borrow before the call.
  • The second borrow is an index or a get_mut into the same container: split with split_at_mut, or restructure the access as one pass with iter_mut.
  • The second borrow is a method call on the original value: do the work through the held borrow, or end the borrow before the call.
  • The diagnostic names a struct's two fields: check whether the borrows are really field borrows — those may coexist — or whether one of them went through a method or an index, which borrows the whole value.
  • No static restructuring fits and the pattern is sound at run time: RefCell, with its panic-on-overlap behavior accepted as the price.

Checklist

  • The three labels in the diagnostic read as a timeline: first borrow, second borrow, and the later use that keeps the first one alive.
  • The overlap classified before editing: sequential uses that merely look simultaneous, a call that can take a reborrow, two parts of one container, or a method call on a borrowed value.
  • Sequential cases fixed by reordering or by a block, with the fixed program's output showing both pieces of work still done.
  • Container cases fixed with a split whose two halves cover the indices the original code used, and the output checked for both updates.
  • No later use deleted to make the error go away, and no clone added where a borrow fix was available.
  • The fixed program run, and its output checked against what the failing program was trying to compute.

Sources

  • Rust, Error codes index: E0499 — the definition of the error: a variable was borrowed as mutable more than once, with the two-bindings example.
  • The Rust Programming Language, References and Borrowing — the rule that a mutable reference excludes all other references, a reference's scope ending at its last use, and the data-race reason for the restriction.
  • std, primitive slice — split_at_mut dividing one mutable slice into two at an index, its panic on an out-of-range index, and split_mut for predicate-separated groups.
  • std, cell::RefCell — borrowing checked at run time instead of compile time, and borrow_mut panicking when the value is currently borrowed.

Keep Exploring