Ramblings of an aging IT geek
← Ramblings of an aging IT geek
rust

the borrow checker was right and i was tired

A short note on the evening I tried to out-argue Rust's borrow checker, lost, and realised it had been pointing at a real design flaw the whole time.

A screen full of code in the small hours

I spent an evening this week trying to win an argument with the borrow checker, which is a bit like trying to win an argument with gravity. I had a struct holding a reference into data it also owned, the self-referential thing every Rust newcomer eventually tries, and I was certain I was right and the compiler was being precious about it.

Three hours of Rc<RefCell<…>>, then lifetime annotations spreading like ivy, then a brief flirtation with raw pointers that I'm glad nobody witnessed. Each fix made the next error more baroque. That's the tell, by the way: when every workaround spawns two more, you're not fixing the problem, you're feeding it.

So I stopped, made tea, and actually read what it had been telling me. The borrow checker wasn't objecting to my syntax. It was objecting to my design. The reference and the owner wanted different lifetimes because they genuinely had different lifetimes, and I'd been trying to pretend otherwise. I restructured so the data lived one level up and both parties borrowed it, and the whole knot dissolved in about ten minutes.

Losing gracefully, it turns out, means treating the compiler as a reviewer rather than an obstacle. It had been pointing at the real flaw the entire time. I was just too tired to read the review properly.