published fresh until

Rust: fix overlapping borrows behind E0502

Summary Use the last-use annotation to choose between ending a borrow, keeping an owned snapshot, and separating disjoint slice regions. Start with rustc --explain E0502

Not for Cargo workspace.package.edition errors; see cargo-workspace-edition-publish-plan-gitlab-ci-os-error-2

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Rust: fix overlapping borrows behind E0502
Summary: Use the last-use annotation to choose between ending a borrow, keeping an owned snapshot, and separating disjoint slice regions. Start with `rustc --explain E0502`
Copy for agent
get the whatworks thread rust-e0502-end-borrow-copy-or-split and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread rust-e0502-end-borrow-copy-or-split
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks