Rust question: Why doesn't Rust have a `RandomAccessIterator`? It seems to be there at some point but got removed: <https://www.cs.brandeis.edu/~cs146a/rust/doc-02-21-2015/std/iter/trait.RandomAccessIterator.html>.

And what is the alternative? Implementing `Index` for the iterator?

#rust #rustlang

std::iter::RandomAccessIterator - Rust

API documentation for the Rust `RandomAccessIterator` trait in crate `std`.

@lesley what does it even mean for an iterator to be random-access? Methinks, random-access and iteration are polar opposites. C++ people in their infinite wisdom seemingly just decided to call their container abstraction an iterator, and their iterators - ranges.
@hjvt It is still useful as an abstraction to have something that is both lazy and indexable. For example, I currently have `grid.rows()` and `grid.columns()` methods that returns iterators, but it would be great if they could be indexed into directly
@lesley is skip + next not good enough? I feel like calling it indexing is very misleading, since it still requires evaluating every element up to index + 1.
@hjvt In my grid case, it is possible to go to the index without any additional evaluation directly
@lesley should you be returning iterators at all then, if you already have something indexable?

@hjvt 🤔I am certainly not iterating but I am already using the lazy property of the iterator.

As I said, I have a specific use case: `grid.column()[index]` in mind. I can also provide a `grid.nth_column(index)` method instead but I wonder whether this is possible

@lesley depends on what your grid is. Also, I kinda forgot but iterators already have `nth` method which can be specialized for your iterator to index directly rather than iterate all elements up to n.
@hjvt Interesting. Thanks a lot!
@hjvt @lesley i came here to mention nth() as well :)
@hjvt @lesley also if you’re writing generic code you can probably expect nth to be fast for an ExactSizeIterator, though you can certainly construct a counter example. There is no marker trait I believe that says nth-is-fast explicitly.
@hjvt @lesley C++ calls a container an iterator? Huh? That is a nonsensical statement.

@hjvt @lesley perhaps there is confusion afterall many things in C++ do have convoluted origins.

Maybe this brief story will clear things up.

https://www.jmeiners.com/efficient-programming-with-components/09_iterators.html#History-of-iterators

9. Iterators

@jaeger @hjvt Interesting. "Coordinate" is certainly a more fitting name for C++ iterators.

C++ ranges (at least from the design of C++20) are also not directly the iterator patterns since they also include containers. Range views are probably the closest to iterators in other languages

@lesley i created a structure with one element, a usize index, and a PhantomData element to bind the lifetime of the iterator to the issuing array. Odd to not have a built in like this but it works for me
@lesley i think a good reason is to avoid aliasing. for example suppose you have a mutable vector `mut v: Vec<i32>`, and you call `.iter_mut()` on it. making the resulting iterator "random access" would allow you to call it twice with possibly the same index, which would violate the borrow checker