# iter-overeager-cloned

Delay iterator cloning until after operations that discard elements

Cloning iterator values before filtering or selection may clone values that the operation discards.
Instead, you SHOULD apply `cloned` after filtering or limiting adapters and clone only values returned by terminal selectors.

- Category: performance
- Level: warning
- Fix: suggestion
- Scope: module

## Reported

```ds title="main.ds"
import { Iterator } from "destack:iter";

struct Label {
    values: ^int32[];
}

function prefix(values: Iterator<&readonly Label>, count: isize): Iterator<Label> {
    return values.cloned().take(count);
}
```

## Accepted

```ds title="main.ds"
import { Iterator } from "destack:iter";

struct Label {
    values: ^int32[];
}

function prefix(values: Iterator<&readonly Label>, count: isize): Iterator<Label> {
    return values.take(count).cloned();
}
```

## Prior art

- [Clippy · iter_overeager_cloned](https://rust-lang.github.io/rust-clippy/master/index.html#iter_overeager_cloned)

[language/linter/src/rules/performance/iter_overeager_cloned.rs:12](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/performance/iter_overeager_cloned.rs#L12)
