# iter-cloned-collect

Prefer direct collection copying over iterator collection

Iterating a borrowed contiguous collection, copying every element, and collecting them rebuilds the same owned array indirectly.
Instead, you SHOULD create the owned array directly from the collection.

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

## Reported

```ds title="main.ds"
struct Label {
    values: ^int32[];
}

function copy(values: &readonly Label[]): Label[] {
    return values.iterator().cloned().toArray();
}
```

## Accepted

```ds title="main.ds"
struct Label {
    values: ^int32[];
}

function copy(values: &readonly Label[]): Label[] {
    return values.clone();
}
```

## Prior art

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

[language/linter/src/rules/performance/iter_cloned_collect.rs:8](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/performance/iter_cloned_collect.rs#L8)
