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

Iterator Indexing #73482

Open
jswrenn opened this issue Jun 18, 2020 · 3 comments
Open

Iterator Indexing #73482

jswrenn opened this issue Jun 18, 2020 · 3 comments
Labels
A-iterators Area: Iterators C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@jswrenn
Copy link
Member

jswrenn commented Jun 18, 2020

Itertools is considering a PR introducing iterator indexing, implemented in the same manner as the core library's slice indexing mechanism. This playground link demonstrates the gist of it:

// `Itertools::get` indexes an iterator:
assert_eq!(
    (0..5).get(2).collect::<Vec<_>>(),
    vec![2]
);

assert_eq!(
    (0..5).get(..3).collect::<Vec<_>>(),
    vec![0, 1, 2]
);

assert_eq!(
    (0..5).get(2..).collect::<Vec<_>>(),
    vec![2, 3, 4]
);

I'm excited to merge this—it feels like something that ought to be in the standard library—but I want to avoid another {Itertools,Iterator}::flatten debacle; i.e., if the core library later adopts this same method, then itertools users calling this method will have builds break.

Would this addition be a good fit for the core library?

@jonas-schievink jonas-schievink added A-iterators Area: Iterators C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jun 18, 2020
@leonardo-m
Copy link

Sounds awesome. It's similar to python itertools.islice function. But I'd like to use the normal array slicing syntax. After this we need a built-in way to match on iterators, and the basic needs are covered.

@cuviper
Copy link
Member

cuviper commented Jun 18, 2020

But I'd like to use the normal array slicing syntax.

The problem is that operator Index only returns &Output, and IndexMut returns &mut Output, so you can't create a new value for the indexing result.

@lachlansneff
Copy link

An IndexableIter trait seems like an interesting idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-iterators Area: Iterators C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants