rust-lang / rfcs

RFCs for changes to Rust
https://rust-lang.github.io/rfcs/
Apache License 2.0
5.92k stars 1.57k forks source link

Make an Iterable trait to make it quicker for a programmer to iterate over something #524

Closed Kilobyte22 closed 8 years ago

Kilobyte22 commented 9 years ago

Currently when we want to iterate over an Array we always have to call .iter() on that array. I suggest that there should be a trait to standardize this:

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

Now, rustc would check in for loops for instances of Iterable and automatically adding a .iter(). This would allow for code like this:

let a = ["a", "b", "c"];
for element in a {
  println!("Element: {}", element);
}

The old way would obviously still work, this would only be syntactic sugar

steveklabnik commented 9 years ago

IIRC such a trait requires higher kinded types, which is why we don't have it yet.=

Kilobyte22 commented 9 years ago

Actually, that does sound quite reasonable, didn't think of that. We'll see what the future brings and hopefully soon this will be possible

sfackler commented 9 years ago

See also #17

Kilobyte22 commented 9 years ago

Ah, okay i did search for this kind of thing, but didn't see #17.

kevinmehall commented 9 years ago

See also the approved RFC 235.

alexcrichton commented 8 years ago

I believe this was done via IntoIterator, so closing.