2 ms·
Yes, this is controversial. Rust requires accepting that it operates on broad rules, and even changing design patterns to fit them. It's unacceptable to some C+
by pornel 6y ago
Yes, this is controversial. Rust requires accepting that it operates on broad rules, and even changing design patterns to fit them. It's unacceptable to some C++ programmers.
OTOH if you roll with this, it makes things simpler. You get uniform rules and guarantees for all interfaces. You don't need to evaluate correctness in detail on a case-by-case basis "is this code safe in this situation?".
It also helps with fearless refactoring, since you can't have these unobvious dependencies on implementation details. You can change a VecDeque to a plain Vec, and if it still compiles, it's still safe.
- millstone 6y agoYeah, 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-...