Skip to content

Chain::nth(0) and next() disagree on a non-fused iterator: Skip::advance_by keeps its skip count #164480

Closed

Description

@hazohelet

Live Reproducer: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4c75f7c599fad96a4ea6f44ec5b37524

Running the following code shows that nth(0) and next() of the same fresh iterator return different values. nth(0) should return what next() returns.

/// A legal non-fused iterator: `None` once, then `Some(1)`, `Some(2)`, ...
struct Gap(u32);

impl Iterator for Gap {
    type Item = u32;
    fn next(&mut self) -> Option<u32> {
        self.0 += 1;
        if self.0 == 1 { None } else { Some(self.0 - 1) }
    }
}

fn main() {
    let next = Gap(0).skip(1).chain(std::iter::empty()).next();
    let nth0 = Gap(0).skip(1).chain(std::iter::empty()).nth(0);
    println!("{:?} {:?}", next, nth0);
}

Output (wrong):

None Some(2)

Expected:

None None

Notes:

  • Only non-fused iterators are affected.
  • Without the chain, nth(0) and next() agree.
  • Present since 1.57.

rustc version:

rustc 1.99.1 (58cf8f9c3 2026-10-13)

This reproduces with the current nightly (rustc 1.101.0-nightly (4ebd258 2026-10-15)).

Activity

  1. added
    C-bug
    on Oct 17, 2026
  2. added
    needs-triage
    on Oct 17, 2026
  3. added
    A-iterators
    T-libs
    and removed
    needs-triage
    on Oct 17, 2026
  4. cuviper commented on Oct 17, 2026

    @cuviper
    Member

    I think Chain is not to blame here, because you can reproduce the same error by using manual advance_by(0); next() -- although that requires nightly since it's unstable.

    #![feature(iter_advance_by)]
    
    /// A legal non-fused iterator: `None` once, then `Some(1)`, `Some(2)`, ...
    struct Gap(u32);
    
    impl Iterator for Gap {
        type Item = u32;
        fn next(&mut self) -> Option<u32> {
            self.0 += 1;
            if self.0 == 1 { None } else { Some(self.0 - 1) }
        }
    }
    
    fn main() {
        let next = Gap(0).skip(1).next();
    
        let mut iter = Gap(0).skip(1);
        iter.advance_by(0).unwrap();
        let nth0 = iter.next();
    
        println!("{:?} {:?}", next, nth0);
    }

    playground

    Skip::advance_by keeps its skip count

    Right, this is the problem. In this scenario, Skip::advance_by(0) calls Gap::advance_by(1) and gets Err(1). It then counts advanced_inner as 0, so self.n stays the same, and it can only return Ok(()) itself for advance_by(0).

    In contrast, Skip::next and Skip::nth always clear self.n.

  5. cuviper commented on Oct 17, 2026

    @cuviper
    Member

    Actually, even if Skip::advance_by(0) did clear self.n, it would instead return Some(1) on the following next(), still wrong.

    Maybe it needs to avoid doing anything in this case? Or maybe this is evidence that advance_by(0) is generally nonsense.

  6. ChrisDenton commented on Oct 17, 2026

    @ChrisDenton
    Member

    Or maybe this is evidence that advance_by(0) is generally nonsense.

    I mean I would agree that is nonsense in any case. But this feels like ZSTs in the sense that we have to decide what's sensible to do in "nonsense" situations.

    I think making it no-op is the least bad option but I don't know if I'm missing a reason not to.

  7. the8472 commented on Oct 18, 2026

    @the8472
    Member

    This seems like a good reason to make the n a NonZeroUsize then.

  8. added 6 commits that reference this issue on Oct 20, 2026
    14903bd
    22606b6
    644ff9d
    f5dc191
    5c07a92
    56d3fef
  9. added a commit that references this issue on Oct 21, 2026
    fe5dc9e
  10. added a commit that references this issue on Oct 21, 2026
    a548214
  11. added a commit that references this issue on Oct 22, 2026
    9c2a16e
  12. added a commit that references this issue on Oct 22, 2026
    497ce99
  13. added a commit that references this issue on Oct 25, 2026
    fb0a8c4
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

    A-iteratorsC-bugT-libs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions