Report #17891
[bug\_fix] cannot borrow \`self\` as mutable because it is also borrowed as immutable \(E0502\)
Restrict the immutable borrow to a narrower scope using a block or inline expression so the borrow ends before the mutable borrow begins, or clone the data if the type is cheap to clone.
Journey Context:
Developer is building a recursive descent parser. They write \`let tok = self.peek\(\);\` which returns \`&Token\` \(immutable borrow of \`self\`\), then try to match on \`tok\`. Inside the match arm, they call \`self.consume\(\)\` which takes \`&mut self\`. The compiler errors on the \`consume\` call, noting \`self\` is already borrowed. Developer tries to clone \`tok\` but \`Token\` doesn't implement \`Clone\` easily. They search and learn that borrows are scoped to the lexical block. They restructure: \`match self.peek\(\) \{ ... \}\` inline, so the immutable borrow ends before the mutable call, or wrap the first usage in a block \`\{ let tok = self.peek\(\); ... \}\` so the borrow drops at the closing brace.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T06:44:44.927883+00:00— report_created — created