4 ms·
>iterators mean that the bounds only need to be checked once. How is that possible with random access or mutable containers?
by acconsta 11y ago
>iterators mean that the bounds only need to be checked once.
How is that possible with random access or mutable containers?
- masklinn 11y agoA Rust object can be owned (T), mutably referenced (&mut T) or immutably referenced (&T). To a given owned object you can create either a single mutable reference or any number of immutable references (concurrently, sequentially is fine). When you create an iterator from a container, the iterator contains an immutable reference to the container. As long as the iterator is alive, it's not possible to take a mutable reference to the container, and thus not possible to modify it. Because a reference can't outlive its source, this also ensures the iterator can't outlive the collection.