Skip to content
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

Naming guidelines recommend into_iter() method instead of IntoIterator trait #206

Open
Aloso opened this issue Nov 14, 2019 · 3 comments
Open
Labels
accepted An amendment that's been accepted and can be applied amendment Amendments to existing guidelines I-nominated Indicates that an issue has been nominated for discussion during a team meeting.

Comments

@Aloso
Copy link

Aloso commented Nov 14, 2019

C-ITER recommends implementing three methods for collections: iter(), iter_mut() and into_iter(). It fails to mention that into_iter() can (and probably should?) be implemented by implementing the IntoIterator trait.

Additionally, the Examples section has a broken link to https://doc.rust-lang.org/std/vec/struct.Vec.html#method.into_iter instead of https://doc.rust-lang.org/std/vec/struct.Vec.html#impl-IntoIterator.

@KodrAus KodrAus added the amendment Amendments to existing guidelines label Dec 21, 2020
@KodrAus
Copy link
Contributor

KodrAus commented Dec 21, 2020

You don't technically need to use the IntoIterator trait in order to work with for loops (it just expects an into_iter method), but we don't actually appear to have any collections that use an inherent into_iter method. The closest is BinaryHeap::into_iter_sorted, which generated some discussion about naming in an ultimately closed PR to stabilize it. I think the conclusion we came to there was that additional iterator methods like into_iter_sorted should use the iter, iter_mut and into_iter prefixes.

@KodrAus
Copy link
Contributor

KodrAus commented Dec 21, 2020

We should probably also mention implementing IntoIterator for &Self and &mut Self too that mirrors the inherent iter and iter_mut methods.

@KodrAus KodrAus added I-nominated Indicates that an issue has been nominated for discussion during a team meeting. accepted An amendment that's been accepted and can be applied labels Dec 21, 2020
@Aloso
Copy link
Author

Aloso commented Dec 23, 2020

You don't technically need to use the IntoIterator trait in order to work with for loops

@KodrAus that isn't true. For example:

struct Foo;

impl Foo {
    fn into_iter(self) -> Bar {
        Bar
    }
}

struct Bar;

impl Iterator for Bar {
    type Item = i32;

    fn next(&mut self) -> Option<i32> {
        Some(42)
    }
}

fn main() {
    for _ in Foo {
        break;
    }
}

This doesn't compile, because Foo is not an iterator. If the IntoIterator trait is implemented, however, it works.

P.S. I guess you meant that you can write for _ in Foo.into_iter() {}, but that's not very idiomatic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted An amendment that's been accepted and can be applied amendment Amendments to existing guidelines I-nominated Indicates that an issue has been nominated for discussion during a team meeting.
Projects
None yet
Development

No branches or pull requests

2 participants