]> git.lizzy.rs Git - rust.git/blob - src/docs/same_item_push.txt
Add iter_kv_map lint
[rust.git] / src / docs / same_item_push.txt
1 ### What it does
2 Checks whether a for loop is being used to push a constant
3 value into a Vec.
4
5 ### Why is this bad?
6 This kind of operation can be expressed more succinctly with
7 `vec![item; SIZE]` or `vec.resize(NEW_SIZE, item)` and using these alternatives may also
8 have better performance.
9
10 ### Example
11 ```
12 let item1 = 2;
13 let item2 = 3;
14 let mut vec: Vec<u8> = Vec::new();
15 for _ in 0..20 {
16    vec.push(item1);
17 }
18 for _ in 0..30 {
19     vec.push(item2);
20 }
21 ```
22
23 Use instead:
24 ```
25 let item1 = 2;
26 let item2 = 3;
27 let mut vec: Vec<u8> = vec![item1; 20];
28 vec.resize(20 + 30, item2);
29 ```