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

Document what happens if std::iter::Take's n is greater than the amount of elements #61222

Closed
KizzyCode opened this issue May 26, 2019 · 8 comments
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-iterators Area: Iterators C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@KizzyCode
Copy link

The documentation of std::iter::Take gives no information about what happens if the underlying iterator yields less elements than Take should take.
Does it return less than n elements, does it panic, ...

Maybe we should change

An iterator that only iterates over the first n iterations of iter.

to something like

An iterator that only iterates over the first n iterations of iter. If iter yields less elements than n, only the amount of elements produced by iter will be returned.

However english is not my mother tongue and I'm pretty sure there is a much better formulation than this (even to my ears this sounds clumsy)^^

@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. A-iterators Area: Iterators labels May 26, 2019
@czipperz
Copy link
Contributor

Well I believe it is correct right now. It just returns the result of self.iter.next(). It doesn't really care what it returns. Some iterators restart iteration after one None, for example Fuse

@ghost
Copy link

ghost commented May 27, 2019

for example Fuse

unless I'm missing something, that Fuse example is flawed ie. panics when overflow. (in playground, switch from Debug to Release for a possible fix)

@Mark-Simulacrum
Copy link
Member

There's not really a notion I think of the amount of elements an iterator returns -- i.e., take will limit the amount of times Some could be returned but other than that it'll simply call next on the underlying iterator. We could perhaps provide an example that None is still returned, e.g., something like the following, but I'm not sure it's super helpful since None is the only thing that could really be returned.

let v = vec![1, 2];
let mut iter = v.into_iter().take(5);
assert_eq!(iter.next(), Some(1));
assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next(), None);
...

@Mark-Simulacrum Mark-Simulacrum removed the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Jun 4, 2019
@KizzyCode
Copy link
Author

Well, my problem (as a foreign speaker) is that the function is not called take_up_to, but take, which (at least to me) implies some sort of "guarantee" that it would take exactly n elements.

While within the iterator-logic the behavior is completely logical, for me in the first minutes it was not.

@czipperz
Copy link
Contributor

czipperz commented Jun 5, 2019

Yep. I agree it is kinda weird

@jonas-schievink jonas-schievink added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Mar 6, 2020
@poliorcetics
Copy link
Contributor

@KizzyCode I made a PR for that, is the added explanation clear from you point of view ?

JohnTitor added a commit to JohnTitor/rust that referenced this issue May 29, 2020
…shtriplett

Clarify the documentation of `take`

This PR addresses the concerns of rust-lang#61222, adding an example for the behaviour of `Iterator::take` when there are less than `n` elements.
@poliorcetics
Copy link
Contributor

The PR fixing this has been merged, this could be closed.

@jieyouxu
Copy link
Member

Closing as docs were updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-iterators Area: Iterators C-enhancement Category: An issue proposing an enhancement or a PR with one. 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

6 participants