RS.CLIPPY.SAME_ITEM_PUSH
The same item is pushed inside of a for loop
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: same_item_push. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks whether a for loop is being used to push a constant value into a Vec.
Why is this bad?
This kind of operation can be expressed more succinctly with
vec![item; SIZE] or vec.resize(NEW_SIZE, item) and using these alternatives may also
have better performance.
Example
let item1 = 2;
let item2 = 3;
let mut vec: Vec<u8> = Vec::new();
for _ in 0..20 {
vec.push(item1);
}
for _ in 0..30 {
vec.push(item2);
}
Use instead:
let item1 = 2;
let item2 = 3;
let mut vec: Vec<u8> = vec![item1; 20];
vec.resize(20 + 30, item2);
Configuration
-
msrv: The minimum rust version that the project supports. Defaults to therust-versionfield inCargo.toml(default:
current version)