Skip to content

IteratorExt: by_ref + take_while consumes one extra element #22802

@oli-obk

Description

@oli-obk

take_while also consumes the first element that does NOT fullfill the condition.

fn main() {
    let a = [1, 2, 3, 4, 5];
    let mut it = a.iter();
    for _ in it.by_ref().take_while(|a| **a < 3) {}
    for i in it {
        println!("{}", i);
    }

    let a = [1, 2, 3, 4, 5];
    let mut it = a.iter();
    for _ in it.by_ref().take(3) {}
    for i in it {
        println!("{}", i);
    }
}

prints

4
5
4
5

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