How to Avoid Fighting Rust Borrow Checker

52 points by ucirello


bitshift

Deferred mutation is not "just a workaround for borrow checker". Treating mutation as data also has other benefits:

The author goes on to mention several bits of added functionality that are easier with mutations-as-data, such as adding logging or parallelism. Which is true, but what it eliminates is just as important: certain classes of bugs.

I play an old DOS game that has various weird bugs whenever a creature dies: e.g., kill this monster and an unrelated segment of wall shifts forward. The reason is because there's an array of all the objects on screen, and that array is mutated concurrently with the objects running their scripts (such as one object killing another), causing array indexes to get mixed up for a frame. No fighting the borrow checker in Turbo Pascal! But a borrow checker would have encouraged a cleaner design, such as setting an is_deleted boolean somewhere and deleting the array entries only after iteration had finished.

wrs

This is great! I wish I had seen this closer to the start of my Rust journey.

The Traps to Developers post is also great, a lot of hard-won knowledge in a small space.

jasonjmcghee

I don't think I use the command queue pattern enough. It really is great for observability. I imagine there's merge / coalesce / replace optimizations that could be done in certain situations as well, so you end up spending less total compute... Also could help with hot path stuff by colocating writes.

What are some reasons why command queue is bad that come to mind for people?