Clean build, but what did it trade?
You added a `cache` field to a Rust service and asked the agent to fix the borrow-checker errors. Five minutes later: `cargo build` clean, tests green. The diff adds three `.clone()` calls and swaps two `?` for `.unwrap()`. It also turns `cache: Cache` into `cache: Rc<RefCell<Cache>>`. Nothing looks like `unsafe` or `#[allow]`. Is this finished?
Safe Rust that moves the error to run time
The agent's proxy signal is a clean `cargo build`. All it can see is the compiler's list of borrow errors, and each of these edits is the shortest one that makes an error vanish. rustc's own help text often suggests cloning. Unlike `unsafe`, these edits are ordinary safe Rust, so they don't look like escapes. But each one either moves a check from compile time to run time or changes what the code means. Later cost: panics, runtime borrow failures, updates lost on copies.
Four tells and what each one trades
- `.unwrap()`/`.expect()` on parsing, I/O, env vars, network or lookups: panics on `Err`/`None` and kills the thread (the process if it's `main`). Fine in tests or for a stated invariant.
- `.clone()` exactly where a borrow error was, especially if the clone is then mutated: edits hit a copy.
- `Rc<RefCell<_>>`: borrow rules are checked at run time, and an overlapping borrow panics.
- `Arc<Mutex<_>>`: a second lock on the same thread deadlocks (waits forever) or panics.
A RefCell that panics only when stock is low
The agent shared an `Inventory` by wrapping it in `Rc<RefCell<_>>`. In `sell`, it holds `let inv = self.inv.borrow();`. When stock is low, it then calls `self.restocker.top_up()`, which calls `borrow_mut()` on the same cell. That panics with `already borrowed: BorrowMutError`, but only on the low-stock path, which no test hit. BETTER: one owner holds `Inventory`. `sell` works out what to restock, ends its borrow, then mutates through `&mut`.
The credit that lands on a copy
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(())
}
Ask what the clean compile was bought with
In Rust, a build made to pass with `.unwrap()`, `.clone()`, `Rc<RefCell<_>>` or `Arc<Mutex<_>>` often just moves the error to run time. It comes back as a panic, a runtime borrow failure, a deadlock, or an edit on a copy. For each one, ask three things. Can it fail here? Does the copy need writing back? Who should own this data?
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.