RS.CLIPPY.MAP_ENTRY
Use of `contains_key` followed by `insert` on a `HashMap` or `BTreeMap`
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: map_entry. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks for usage of contains_key + insert on HashMap
or BTreeMap.
Why is this bad?
Using entry is more efficient.
Known problems
The suggestion may have type inference errors in some cases. e.g.
let mut map = std::collections::HashMap::new();
let _ = if !map.contains_key(&0) {
map.insert(0, 0)
} else {
None
};
Example
if !map.contains_key(&k) {
map.insert(k, v);
}
Use instead:
map.entry(k).or_insert(v);