Ad Space — Top Banner

E0502

Rust Programming Language

Severity: Minor

What Does This Error Mean?

Rust error E0502 means you tried to borrow something as mutable while an immutable borrow of it already exists. Rust only allows one mutable borrow at a time, and no mutable borrows while any immutable borrows are active. This rule prevents data races and unexpected changes to values that are being read. Fix it by making sure immutable borrows are finished before taking a mutable borrow.

Affected Models

  • Rust 1.0 and later
  • All Rust editions (2015, 2018, 2021)
  • cargo build and rustc

Common Causes

  • Holding an immutable reference (&value) while also trying to modify the same value
  • Storing a reference to a collection item and then trying to push or modify the collection
  • Creating an immutable borrow inside a loop that outlasts the loop body
  • Passing a value to a function by reference while also passing it mutably to another function
  • Keeping a reference alive in a variable that is still in scope when a mutable borrow is attempted

How to Fix It

  1. Identify where the immutable borrow starts and where it ends. Look at the scope (curly braces) where the reference lives.

    In Rust, a borrow ends when the variable holding the reference goes out of scope or is no longer used.

  2. Move the mutable borrow to after the immutable borrow is finished. If possible, drop the immutable reference first.

    You can explicitly drop a reference by calling drop(my_ref) or by introducing a new block with {} to limit its scope.

  3. If you read a value from a collection and then want to modify the collection, copy the value out first instead of holding a reference.

    Example: let val = my_vec[0].clone(); — now you own a copy and can mutate my_vec freely.

  4. Restructure the code so that reading and writing happen in separate blocks. Finish all reads, then do the write.

    Wrapping the read in its own {} block ensures the reference is dropped before the mutable access.

  5. Consider using index-based access instead of references for collections when you need to modify while iterating.

    Using an index (my_vec[i]) instead of a reference lets you avoid holding a borrow across a modification.

When to Call a Professional

E0502 is a compile-time borrow checker error. Your code is safe — the compiler is preventing a real bug. The fix usually involves restructuring code so borrows do not overlap. The compiler error shows which borrow is immutable, where it started, and where the conflicting mutable borrow is.

Frequently Asked Questions

Why does Rust not allow a mutable and immutable borrow at the same time?

If you could read and write the same value simultaneously, the reader might see the value change unexpectedly mid-read. In multi-threaded code this is a data race, which causes unpredictable and dangerous bugs. Rust prevents this at compile time so the problem can never occur at runtime.

What are the exact borrow rules in Rust?

You can have any number of immutable borrows (&T) at the same time. Or you can have exactly one mutable borrow (&mut T). But you cannot have both a mutable borrow and any immutable borrow at the same time. These rules are checked at compile time by the borrow checker.

Does this error ever happen with Rust 2021 edition non-lexical lifetimes?

Non-lexical lifetimes (NLL) made many borrow errors go away by ending borrows earlier than the end of a block. But E0502 can still happen when the compiler cannot prove that the borrows do not overlap. If you upgraded from an older Rust edition and still see E0502, restructuring the code is still the right fix.