Skip to content

Implicitly copyable iterators are mega confusing #18045

Closed
@kmcallister

Description

@kmcallister

For example, this code:

fn main() {
    let stream = "Hello, world!".chars().cycle();
    for _ in range(0u, 10) {
        let chunk: String = stream.take(3).collect();
        println!("{}", chunk);
    }
}

compiles and prints Hel ten times, rather than the expected Hel, lo,, wo, etc.

The fact that stream isn't mut is a clue that this code doesn't work as expected.

I guess the solution is stream.as_ref().take(3) but the "default" behavior here is really confusing.

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