Rust

Rust

By DeepSource

Construction of Iterator from single-element collection RS-W1200

Anti-pattern
Autofix

Construction of single-element collections, such as arrays or vectors, for the sole purpose of iteration, can be detrimental to performance. Consider using std::iter::once instead.

Examples

Bad practice

let mut v = vec![1, 2, 3];

for x in v.chain([4]) {}

Recommended

let mut v = vec![1, 2, 3];

for x in v.chain(std::iter::once(4)) {}