Skip to content

repeat_with() can return Iterator + Copy #145603

@dohse

Description

@dohse

I tried this code:

let mut i = 0;
let iterator = repeat_with(move || {
    i += 1;
    i
});
let _ = iterator.take(1);
let _ = iterator.take(1);

I expected to see this happen:

This should fail to compile with an error like "use of moved value: iterator"

Instead, this happened:

It compiles and creates a copy of the iterator.

Meta

rustc --version --verbose:

rustc 1.89.0 (29483883e 2025-08-04)
binary: rustc
commit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2
commit-date: 2025-08-04
host: x86_64-unknown-linux-gnu
release: 1.89.0
LLVM version: 20.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-iteratorsArea: IteratorsC-bugCategory: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions