RS.CLIPPY.MANUAL_RETAIN

`retain()` is simpler and the same functionalities

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: manual_retain. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for code to be replaced by .retain().

Why is this bad?

.retain() is simpler and avoids needless allocation.

Example

let mut vec = vec![0, 1, 2];
vec = vec.iter().filter(|&x| x % 2 == 0).copied().collect();
vec = vec.into_iter().filter(|x| x % 2 == 0).collect();

Use instead:

let mut vec = vec![0, 1, 2];
vec.retain(|x| x % 2 == 0);
vec.retain(|x| x % 2 == 0);

Configuration

  • msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml

    (default: current version)