3 ms·
Right. Iterator invalidation rules are important yet only checked indirectly, through tools like ASAN or VC++ debug iterator support. It's a big problem. But R
by millstone 6y ago
Right. Iterator invalidation rules are important yet only checked indirectly, through tools like ASAN or VC++ debug iterator support. It's a big problem.
But Rust has its conventions too. Probably you would satisfy the borrow checker through an index instead of a pointer:
let mut xs = vec![1, 2, 3];
let idx = 0;
xs.push(92);
println!("{}", xs[idx]);
This is memory safe, but in an unsatisfying way. Nothing hints to the compiler that idx is connected to the vector. If you inserted into the front rather than the back, you would have a "dangling index". You just have to know to not do that. It's purely by convention too, except there's no tooling support to help.
It turns out programs really do need a way to hold references to collection elements across mutation.
- deleted 6y ago[deleted]
- zbentley 6y ago> This is memory safe, but in an unsatisfying way. How is it unsatisfying? I know that "xs[idx]" isn't going to be garbage data from elsewhere in my program, and that it won't occasionally segfault when I try to print it. That's a big improvement.