# drain-collect

Disallow draining a collection only to collect the same elements again

Draining an entire collection and collecting it into the same representation allocates replacement storage.
Instead, you SHOULD call `take` to move out the existing collection and leave its default value.

`take` transfers the existing allocation to the returned collection, so the emptied source no longer retains its capacity.

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

## Reported

```ds title="main.ds"
function removeAll(values: &exclusive int32[]): int32[] {
    return values.drain().toArray();
}
```

## Accepted

```ds title="main.ds"
import { take } from "destack:memory";

function removeAll(values: &exclusive int32[]): int32[] {
    return take(values);
}
```

## Prior art

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

[language/linter/src/rules/performance/drain_collect.rs:6](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/performance/drain_collect.rs#L6)
