Work effectively with AI coding agents. Understand them as language models running in a tool loop with limited context, which is why they guess, game the visible signal and report confidently. Recognise the shortcut and correctness tells in their code across Python, TypeScript/JavaScript, Rust, Java and Go, and know the damage each one does later. Verify changes instead of trusting them, review diffs fast for quality and security, judge when work is truly finished, and choose, design and steer tasks so agents can finish them well.
Rust tells: unwrap, clone and RefCell to appease the compiler
card 4 of 6
A RefCell that panics only when stock is low
builds on: Silencing the checker: type escapes and suppressions, Error handling idioms across languages
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`.
Terms on this card
- Rc<RefCell<_>>
- Rc gives shared ownership on one thread. RefCell allows mutation through a shared handle, checking borrow rules at run time and panicking on overlap.
- BorrowMutError
- The error behind the panic 'already borrowed' when RefCell::borrow_mut is called while another borrow of the same cell is alive.
- &mut
- A Rust mutable reference: exclusive access that lets you change the original value in place.
- panics
- In Rust, an unrecoverable runtime error that by default stops the current thread with a message.
- RefCell
- Rust type that moves borrow checking from compile time to runtime; a conflicting borrow panics while the program runs.
Lesson goal: Recognise Rust-specific shortcuts: `.unwrap()`/`.expect()` on fallible paths, `.clone()` scattered to dodge borrow errors, and `Rc<RefCell<_>>` or `Arc<Mutex<_>>` used where ownership needed redesigning. Explain the panics and runtime borrow failures these trade for a clean compile. ·
the whole lesson