Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Kilobyte22 opened this issue Dec 14, 2014 · 6 comments

Comments

@Kilobyte22
Copy link

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
Copy link
Member

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

@Kilobyte22
Copy link
Author

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
Copy link
Member

See also #17

@Kilobyte22
Copy link
Author

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

@kevinmehall
Copy link

See also the approved RFC 235.

@alexcrichton
Copy link
Member

I believe this was done via IntoIterator, so closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants