There should be a new trait "Iterable"


#1

We should define a new trait Iterable like this:

pub trait Iterable<T> { fn iter() -> Iterator<T>; }

to organize libraries in a better way.

I’m not sure if we should also define another trait for iter_mut() (IterableMut or MutIterable ?)


In fact I believe

for r in obj { f(r); }

should desugar into

let mut it = obj.iter(); // add .iter()
loop match it.next() {
    Some(r) => { f(r); }
    None => { }
}

rather than

let mut it = obj;
loop match it.next() {
    Some(r) => { f(r); }
    None => { }
}

which is how it’s done now, but that’s another topic I’d like to discuss elsewhere.


#2

This is actually covered by the collections reform RFC, although there are some problems with this approach, e.g. something equivalent to the direct desugaring stops #8372 from ever working.


#3

@huon Wow, thanks, I didn’t pay much attentions to RFCs…


#4

As a side note, you may be interested in the recent detailed write-up on iterators, ranges and containers by Eric Niebler, summarizing the work done by the C++ ranges study group (link). It’s not very C++ specific and have a lot of intersection points with current work on stabilization of containers and iterators in Rust.