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

Behavior of Iterator::zip does not match with documentation when iterator is not fused #50225

Closed
tirr-c opened this issue Apr 25, 2018 · 0 comments
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@tirr-c
Copy link
Contributor

tirr-c commented Apr 25, 2018

Iterator::zip says:

When either iterator returns None, all further calls to next will return None.

However, it is not always true; it actually returns Some after None when the source iterator is not fused.

/// An iterator that yields `None` every `p`-th call on `next`.
struct Foo {
    p: usize,
    c: usize,
}

impl Foo {
    fn new(p: usize) -> Self {
        Foo { p, c: 0 }
    }
}

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

fn main() {
    let a = Foo::new(3);
    let b = Foo::new(5);
    let mut c = a.zip(b);
    
    assert_eq!(c.next(), Some((1, 1)));
    assert_eq!(c.next(), Some((2, 2)));
    assert_eq!(c.next(), None);
    assert_eq!(c.next(), None);  // *
}
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `Some((4, 3))`,
 right: `None`', src/main.rs:34:5

Playground

@pietroalbini pietroalbini 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 labels Apr 26, 2018
frewsxcv added a commit to frewsxcv/rust that referenced this issue May 13, 2018
bors added a commit that referenced this issue May 20, 2018
Fix incorrect statement about return value for Iterator::zip.

Fixes #50225.
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 C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

2 participants