# manual-copy

Replace element-by-element copy loops with a bulk copy operation

Copying corresponding elements one at a time repeats bounds checks and hides a contiguous copy.
Instead, you SHOULD use the collection's bulk copy operation.

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

## Reported

```ds title="main.ds"
function copy(target: &exclusive int32[], source: &readonly int32[]): void {
    for (let index: isize = 0; index < source.length; index++) {
        target[index] = source[index];
    }
}
```

## Accepted

```ds title="main.ds"
function copy(target: &exclusive int32[], source: &readonly int32[]): void {
    target.view(0, source.length).copyFrom(source.view(0));
}
```

## Prior art

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

[language/linter/src/rules/performance/manual_copy.rs:7](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/performance/manual_copy.rs#L7)
