There should be a new trait "Iterable"

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.

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.

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

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.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.