summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/same_item_push.txt
blob: 7e724073f8aa7aa9a5cc4d1916903352fe74c835 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
### 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);
```