4 ms·
Yeah, Rust effectively assumes and then enforces the weakest possible iterator invalidation rules. A C++ programmer may write: vec.erase(iter); and if the
by millstone 6y ago
Yeah, Rust effectively assumes and then enforces the weakest possible iterator invalidation rules. A C++ programmer may write:
vec.erase(iter);
and if the iterator is invalid or from the wrong vector, this will crash, if you are lucky.
Rust won't compile that, so instead you would write:
vec.remove(idx);
This won't crash even if `idx` is from the "wrong vector", or refers to an element that has been moved. So we have improved memory safety, but it hides our mistakes.
It would be really nice in both Rust and C++ to have validity checks here. The closest thing I know of is VC++'s debug iterator support.
https://docs.microsoft.com/en-us/cpp/standard-library/debug-iterator-support https://docs.microsoft.com/en-us/cpp/standard-library/debug-...