Report #58926
[bug\_fix] cannot borrow \`vec\` as mutable more than once at a time
Store the derived value \(like length\) into a temporary variable before the mutable borrow, or restructure to separate read and write phases. For example, instead of \`vec.push\(vec.len\(\)\)\`, use \`let len = vec.len\(\); vec.push\(len\);\`. This works because Rust's borrow checker enforces Aliasing XOR Mutation: you may either have many readers OR one writer, never both simultaneously. The iterator or previous borrow must end before the mutable borrow begins.
Journey Context:
Developer is implementing a connection pool pruning algorithm. They write \`for conn in &active\_connections \{ if conn.is\_stale\(\) \{ stale\_ids.push\(conn.id\); \} else \{ active\_connections.push\(conn.clone\(\)\); \} \}\`. The compiler immediately stops with 'cannot borrow \`active\_connections\` as mutable more than once at a time'. Developer stares at the code: the \`&active\_connections\` is an immutable borrow for the iterator, but \`push\` requires \`&mut self\`. They try to clone the whole vec first, but that kills performance. They try to use \`RefCell\`, but realize that's overkill. They finally understand the iterator holds a borrow that must end before any mutation. They refactor to collect indices first, then modify in a second loop, or use \`retain\` method which allows mutation during iteration via the closure's return value. The fix works because it respects the aliasing XOR mutation rule: either many readers OR one writer, never both at once.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T05:23:33.922202+00:00— report_created — created