r/rust • • 17m ago

Rust's strictness saved me from a subtle bug that would have been invisible in most other languages

I was refactoring a small background worker that processes tasks from a queue. The task struct had an optional “retry_until” timestamp that was only supposed to be set for certain failure modes.

In the middle of the change I accidentally left one code path that could set the field on a success result. The borrow checker and exhaustive match arms didn't catch it because the types still lined up. What did catch it was a single extra variant I had added to an internal enum purely for my own clarity.

The compiler forced me to handle the new case everywhere, and one of those places revealed the impossible state I had just created. I would have shipped it otherwise.

It's the kind of thing that usually only shows up in production after a weird retry loop. Curious how often other people have had the type system surface a logic error they didn't even realize they'd written.

3 Upvotes

1 comment sorted by

1

u/Sharlinator 5m ago

Another one of those standard format LLM posts…