Rust

Rust

Made by DeepSource

Unnecessary collect() on iterable to Vec RS-P1006

Performance
Minor
Autofix

Collecting Iterator to Vec has the possible cost of forcing allocation, hence instead directly using the iterator is more efficient quite often, if the Vec is unnecessary.

Consider replacing,

  • .collect::<Vec<_>>().len() with .count().
  • .collect::<Vec<_>>().is_empty() with .next().is_none().

Bad practice

(0..5).collect::<Vec<_>>().len()

Recommended

(0..5).count()