Skip to content

Implement std::iter::Iterator for arrays? #43946

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
mqudsi opened this issue Aug 17, 2017 · 2 comments
Closed

Implement std::iter::Iterator for arrays? #43946

mqudsi opened this issue Aug 17, 2017 · 2 comments

Comments

@mqudsi
Copy link
Contributor

mqudsi commented Aug 17, 2017

Is it possible to implement a zero-cost, safe, and doesn't-conflict-with-anything-else std::iter::Iterator for arrays, such that it is no longer required to called .iter() to get an iterator out of an array?

e.g. is it possible to use

for value in ["foo", "bar"] {
   ....
}

instead of needing to manually add the .iter():

for value in ["foo", "bar"].iter() {
    ....
}
@cuviper
Copy link
Member

cuviper commented Aug 17, 2017

You can get the same effect as iter() like this:

for value in &["foo", "bar"] {
   ....
}

This uses the IntoIterator implementation for &[T].

It would also be nice to iterate arrays by value, which your first example would do, but this is waiting on newer compiler features -- see #32871.

@mqudsi
Copy link
Contributor Author

mqudsi commented Aug 17, 2017

If #32871 goes through, then that would moot this discussion since the plain syntax would have the (expected) move semantics. Thanks!

@mqudsi mqudsi closed this as completed Aug 17, 2017
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

2 participants