What this thread does
error[E0502]: cannot borrow s as mutable because it is also borrowed as immutable
A shared reference is still needed when the code requests exclusive access to the same value.
Do first: run rustc --explain E0502, then follow the diagnostic's borrow-origin and later-use annotations to decide whether that reference must survive the mutation. Official E0502 explanation, rustc --explain option
Evidence: the official example establishes the conflicting-borrow rule; a September 4, 2025 report illustrates the same conflict through a method receiver. Neither establishes which edit preserves your program's intent. RobertPHeller's report and replies
Not for: Cargo workspace.package.edition errors; see cargo-workspace-edition-publish-plan-gitlab-ci-os-error-2
Diagnostic: E0502
As of November 2026, this recipe targets Rust 1.99.1, released October 15, 2026. These are source edits, not a compiler switch that permits overlapping access. Release note
Choose the branch
1. The reference is only needed before the mutation → end its use first. On Rust 1.99.1, move its final read before the mutable operation, or put the read in a smaller block. A borrow can end at its last use rather than the closing brace. For example:
let mut text = String::from("hello");
let view = &text;
println!("{view}");
text.push('!');
Run cargo check on the affected crate; the confirmation is that the original E0502 location disappears without a later read of view. Official borrowing chapter
2. The old contents must survive the mutation → keep owned data. On Rust 1.99.1, for a String use let snapshot = text.clone(); before text.push('!');, then read snapshot. Clone the owned value, rather than merely cloning a reference. Run cargo check, then exercise the operation and compare the saved pre-change text with the changed text. Cloning may allocate; shared owners can still share underlying state. Rust 1.99.1 Clone documentation, String methods
3. The accesses concern separate slice regions → express the split. On Rust 1.99.1, replace competing indexing borrows with let (left, right) = values.split_at_mut(mid);, where mid <= values.len(), and access each region through its own result. Run cargo check; then exercise the boundary case mid == values.len() and the ordinary case that failed before. This does not permit a reference into a vector to survive a vector-wide operation such as push. Rust 1.99.1 split_at_mut contract, vector borrowing example
Public report and limits
In the September 2025 report, holding a mutable element reference conflicted with a later &self helper call: the receiver also covered the containing collection. quinedot advises, “Pass in an index or a fresh, shared borrow instead.” Replies also suggest narrowing the helper's inputs. That case explains why inspecting only the helper's implementation can be misleading. Report
An owned snapshot changes what later reads observe. A slice split requires genuinely disjoint regions.
Started from a post by @The Rust Project, packaged by the editor. Original post