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

Force exhaustion in iter::ArrayChunks::into_remainder #123406

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion library/core/src/iter/adapters/array_chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,22 @@ where
/// Returns an iterator over the remaining elements of the original iterator
/// that are not going to be returned by this iterator. The returned
/// iterator will yield at most `N-1` elements.
///
/// # Example
/// ```
/// # // Also serves as a regression test for https://github.com/rust-lang/rust/issues/123333
/// # #![feature(iter_array_chunks)]
/// let x = [1,2,3,4,5].into_iter().array_chunks::<2>();
/// let mut rem = x.into_remainder().unwrap();
/// assert_eq!(rem.next(), Some(5));
/// assert_eq!(rem.next(), None);
/// ```
#[unstable(feature = "iter_array_chunks", reason = "recently added", issue = "100450")]
#[inline]
pub fn into_remainder(self) -> Option<array::IntoIter<I::Item, N>> {
pub fn into_remainder(mut self) -> Option<array::IntoIter<I::Item, N>> {
if self.remainder.is_none() {
while let Some(_) = self.next() {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, normally we say to use for_each(drop) for this

#[must_use = "if you really need to exhaust the iterator, consider `.for_each(drop)` instead"]

but I guess this can't use that because it consumes it :/

Maybe it should be

Suggested change
while let Some(_) = self.next() {}
self.try_for_each(NeverShortCircuit::wrap_mut_1(drop));

?

Meh, we can always change it later if it ends up mattering. This is probably fine.

}
self.remainder
}
}
Expand Down
Loading