25 ms·
re: find_or_insert http://static.rust-lang.org/doc/0.8/std/hashmap/struct.HashMap.html#fn.find_or_insert http://static.rust-lang.org/doc/0.8/std/hashmap/struct
by cscottnet 13y ago
re: find_or_insert
http://static.rust-lang.org/doc/0.8/std/hashmap/struct.HashMap.html#fn.find_or_insert http://static.rust-lang.org/doc/0.8/std/hashmap/struct.HashM...
It appears to still have the mandatory copy. (And this is just one example of a pervasive issue with the standard library.)
- dbaupp 13y agoIf that's the problem, it's really rather easy to define a wrapper that only clones on an insert. (And, I fundamentally disagree that over-copying this is a pervasive issue; there are a few places (mainly old code) where unnecessary Clone-ing is performed, but this is very much the exception.) fn find_or_insert_clone<'a, K: Clone, V>(map: &'a mut HashMap<K,V>, key: &K, val: V) -> &'a mut V { match map.find_mut(key) { Some(x) => return x, None => {} } // `key` is definitely missing map.find_or_insert(key.clone(), val) }