Borrow checker fighting a simple loop

Back to list2023-12-12
You

I cannot mutate a vector while iterating over it.

ChatGPT

That is the rule doing its job: a resize would invalidate the iterator. Collect the changes and apply after, or iterate by index, or use retain when you are removing:

items.retain(|item| item.keep);

In C++ the same code compiles and reads freed memory occasionally, which is the difference being paid for.

You
budget-2025.png