Skip to content

Suggest Vec::retain() in cases of assigning a chain of .filter().collect() to the original variable. #8097

@jwannebo

Description

@jwannebo

What it does

Guides users to use Vec::retain() over various permutations of into_iter().filter().collect() if used to assign over the original vector.

Lint Name

vec_retain

Category

style, complexity, perf, pedantic

Advantage

Conciseness, readability, and reuses existing memory (in the .iter()...copied() case)

Drawbacks

Difficulty parsing out particularly complex chains of function calls.

Example

let mut my_vec = vec![0, 1, 2, 3, 4, 5];

my_vec = my_vec.into_iter().filter(|x| x % 2 == 0).collect();
// Or
my_vec = my_vec.iter().filter(|&x| x % 2 == 0).copied().collect();

Could be written as:

let mut my_vec = vec![0, 1, 2, 3, 4, 5];

my_vec.retain(|x| x % 2 == 0);

Metadata

Metadata

Assignees

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions