# manual-iterator-inspect

Prefer Iterator.inspect when mapping observes and returns each value

Mapping an iterator value only to observe it before returning it unchanged obscures the side effect.
Instead, you SHOULD call `inspect` to observe each value without changing the sequence.

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

## Reported

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

declare function record(value: &readonly int32): void;

function observe(values: Iterator<int32>): Iterator<int32> {
    return values.map((value) => {
        record(value);
        value
    });
}
```

## Accepted

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

declare function record(value: &readonly int32): void;

function observe(values: Iterator<int32>): Iterator<int32> {
    return values.inspect((value) => {
        record(value);
    });
}
```

## Prior art

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

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