Iterator items cloned before an adaptor that discards them

ID

rust.performance.iter_overeager_cloned

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Inefficient Api Usage

Language

Rust

Description

cloned copies every item, and the adaptor that follows then drops most of them. Move cloned after the adaptor so only the items that survive are cloned, or drop it entirely when the adaptor consumes the iterator and never needs owned values.

Rationale

cloned copies every item, and the adaptor that follows then drops most of them. Move cloned after the adaptor so only the items that survive are cloned, or drop it entirely when the adaptor consumes the iterator and never needs owned values.

The following code illustrates the pattern detected by this rule:

fn long_names(names: &[String]) -> Vec<String> {
    // FLAGGED: Iterator items cloned before an adaptor that discards them
    names.iter().cloned().filter(|n| n.len() > 8).collect()
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved