:timing
:sccache 1
Tips and Traps¶
Summary of Collections in Rust has a good summary on when to each which collection in Rust.
std::collections::HashMap has a good discussions on implementation details of HashMap and things to note when use it.
BTreeMap is a sorted alternative to HashMap.
hashbrown is a Rust port of Google's high-performance SwissTable hash map, adapted to make it a drop-in replacement for Rust's standard HashMap and HashSet types.
indexmap is a hash table which preserves (in a limited sense) insertion order. It is similar to dict in Python 3.7+.
fnv is an implementation of the Fowler–Noll–Vo hash function.
Fast Hashing Algorithms has a good discussion of fast hashing algorithms.
Hash code and Hash Maps¶
hashbrown is a Rust port of Google's high-performance SwissTable hash map, adapted to make it a drop-in replacement for Rust's standard HashMap and HashSet types since Rust 1.36. However, you might still want to use hashbrown for a few reasons.
- hashbrown uses AHash as the default hasher, which is less secure but much faster than SipHash (default hash used in the Rust standard library).
- It can be used in an environment without std.
fnv is an implementation of the Fowler–Noll–Vo hash function.
phf provides runtime support for perfect hash function data structures
indexmap A hash table with consistent order and fast iteration. The indexmap is a hash table where the iteration order of the key-value pairs is independent of the hash values of the keys. It has the usual hash table functionality, it preserves insertion order except after removals, and it allows lookup of its elements by either hash table key or numerical index.
dashmap is a blazing fast concurrent HashMap for Rust.
TinySet provides a few collections that are optimized to scale in size well for small numbers of elements, while still scaling well in time (and size) for numbers of elements.
schnellru is a fast and flexible LRU map.
For simple integer hashing functions, integer modulo is a very BAD (in terms of speed) hashing function while the multiplicative hashing is a much better alternative.
By default, Rust hash tables use Siphash 1-3, a hash function that is high quality but fairly slow. In contrast, the Rust compiler uses as hash function called FxHasher, which is surprisingly simple yet effective.
FNV Hash and AHash.
std::collections::HashMap¶
use std::collections::HashMap;
let mut map: HashMap<i32, i32> = HashMap::new();
map.get(&1)
map.contains_key(&1)
map.insert(1, 1000)
map.get(&1)
map.get(&1).unwrap()
map.contains_key(&1)
Update the value of the key 1
.
*map.get_mut(&1).unwrap() += 1;
map.get(&1)
with_capacity¶
let mut m2: HashMap<i32, i32> = HashMap::with_capacity(10);
m2
m2.len()
m2.capacity()
m2.insert(1,1);
m2.len()
m2.capacity()
std::collections::BTreeMap¶
use std::collections::BTreeMap;
let mut m: BTreeMap<u32, Vec<usize>> = BTreeMap::new();
m.insert(1000, vec![2, 7]);
m.insert(100, vec![3, 4]);
m.insert(333, vec![36, 21]);
m.get(&0)
m.get_mut(&1000).unwrap().push(111);
m.get(&1000)
Confirm that keys of BTreeMap are sorted.
for k in m.keys() {
println!("{}", k);
}