Skip to content

.by_ref().chunks() consumes one extra element than expected #1035

Open

Description

@ronnodas

If I use iter.by_ref().chunks(n) and consume k of the chunks, I expect exactly k*n items to be consumed from iter. But this is not the case as the failing assertion in the following code (playground link) shows:

fn main() {
    let mut iter = 1_u32..10;
    {
        let chunks = iter.by_ref().chunks(2);
        let chunk = chunks.into_iter().next().unwrap();
        assert_eq!(chunk.count(), 2)
    }

    assert_eq!(iter.next(), Some(3)) // actually yields Some(4)
}

If the number of chunks to be consumed is known ahead of time then we can use by_ref().take().chunks() as a workaround, but this doesn't work in general.

Activity

  1. ronnodas commented on Aug 9, 2026

    @ronnodas
    ContributorAuthor

    I suppose this is because the implementation of chunks() wants to reuse the machinery in ChunkBy, but in general ChunkBy really needs access to the next element to know when a chunk ends. I'm not sure what the right solution is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions