]> git.lizzy.rs Git - rust.git/blob - src/docs/map_entry.txt
Rollup merge of #104595 - compiler-errors:poly-existential-predicate, r=lcnr
[rust.git] / src / docs / map_entry.txt
1 ### What it does
2 Checks for uses of `contains_key` + `insert` on `HashMap`
3 or `BTreeMap`.
4
5 ### Why is this bad?
6 Using `entry` is more efficient.
7
8 ### Known problems
9 The suggestion may have type inference errors in some cases. e.g.
10 ```
11 let mut map = std::collections::HashMap::new();
12 let _ = if !map.contains_key(&0) {
13     map.insert(0, 0)
14 } else {
15     None
16 };
17 ```
18
19 ### Example
20 ```
21 if !map.contains_key(&k) {
22     map.insert(k, v);
23 }
24 ```
25 Use instead:
26 ```
27 map.entry(k).or_insert(v);
28 ```