The credit that lands on a copy
builds on: Silencing the checker: type escapes and suppressions, Error handling idioms across languages
The agent first wrote `self.accts.get(id)` and hit "cannot assign to `a.balance`, which is behind a `&` reference". Its fix is BAD, with two tells. `self.accts[id]` panics on an unknown id, just like `.unwrap()`. `.clone()` makes the credit land on a copy that is dropped at `}`. It compiles, and `self.accts` never changes. Later cost: credits silently lost. BETTER borrows mutably and returns an error for a missing account.
// Rust
// BAD
fn credit(&mut self, id: &str, amt: u64) {
let mut a = self.accts[id].clone();
a.balance += amt;
}
// BETTER
fn credit(&mut self, id: &str, amt: u64)
-> Result<(), LedgerError> {
let a = self.accts.get_mut(id)
.ok_or(LedgerError::NoAccount)?;
a.balance += amt;
Ok(())
}