Report #103785
[bug\_fix] error\[E0502\]: cannot borrow \`v\` as mutable because it is also borrowed as immutable
Restrict the immutable borrow to a smaller scope with braces so it ends before the mutable borrow, or restructure the code so the read and write do not overlap. For example, wrap \`let first = &v\[0\]; println\!\("\{\}", first\);\` in \`\{\}\` before calling \`v.push\(4\);\`.
Journey Context:
I was writing a small parser that peeked at the first element of a Vec, then conditionally pushed more data. The code looked innocent: \`let head = &items\[0\]; if should\_extend\(head\) \{ items.extend\(more\); \}\`. \`cargo check\` exploded with E0502. I stared at the line numbers; the immutable borrow \`head\` was considered alive until the end of the block, which overlapped the mutable borrow inside \`extend\`. I first tried cloning \`head\`, which compiled but felt wasteful. Then I remembered that a borrow's lifetime ends at its last use, not at the end of scope, but the mutable call still happened while \`head\` existed. The clean fix was to put the read inside a nested scope so the immutable reference was dropped before the mutation. The program compiled and the vector logic stayed intact.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-13T04:42:16.955632+00:00— report_created — created