Struct std::collections::hash_map::HashMap
OverviewA hash map implemented with quadratic probing and SIMD lookup.
By default, HashMap
uses a hashing algorithm selected to provide resistance against HashDoS attacks. The algorithm is randomly seeded, and a reasonable best-effort is made to generate this seed from a high quality, secure source of randomness provided by the host without blocking the program. Because of this, the randomness of the seed depends on the output quality of the system's random number coroutine when the seed is created. In particular, seeds generated when the system's entropy pool is abnormally low such as during system boot may be of a lower quality.
The default hashing algorithm is currently SipHash 1-3, though this is subject to change at any point in the future. While its performance is very competitive for medium sized keys, other hashing algorithms will outperform it for small keys such as integers as well as large keys such as long strings, though those algorithms will typically not protect against attacks such as HashDoS.
The hashing algorithm can be replaced on a per-HashMap
basis using the [default
], [with_hasher
], and [with_capacity_and_hasher
] methods. There are many alternative hashing algorithms available on crates.io.
It is required that the keys implement the [EQ
] and [HASH
] protocols. If you implement these yourself, it is important that the following property holds:
k1 == k2 == hash
In other words, if two keys are equal, their hashes must be equal. Violating this property is a logic error.
It is also a logic error for a key to be modified in such a way that the key's hash, as determined by the [HASH
] protocol, or its equality, as determined by the [EQ
] protocol, changes while it is in the map. This is normally only possible through [Cell
], [RefCell
], global state, I/O, or unsafe code.
The behavior resulting from either logic error is not specified, but will be encapsulated to the HashMap
that observed the logic error and not result in undefined behavior. This could include panics, incorrect results, aborts, memory leaks, and non-termination.
The hash table implementation is a Rust port of Google's SwissTable. The original C++ version of SwissTable can be found here, and this CppCon talk gives an overview of how the algorithm works.
Examples
use HashMap;
let m = new;
m.insert;
m = 5;
assert_eq!;
assert_eq!;
assert_eq!;
Methods
Creates an empty HashMap
.
The hash map is initially created with a capacity of 0, so it will not allocate until it is first inserted into.
Examples
use HashMap;
let map = new;
Creates an empty HashMap
with at least the specified capacity.
The hash map will be able to hold at least capacity
elements without reallocating. This method is allowed to allocate for more elements than capacity
. If capacity
is 0, the hash map will not allocate.
Examples
use HashMap;
let map = with_capacity;
Returns the number of elements in the map.
Examples
use HashMap;
let a = new;
assert_eq!;
a.insert;
assert_eq!;
Returns the number of elements the map can hold without reallocating.
This number is a lower bound; the HashMap<K, V>
might be able to hold more, but is guaranteed to be able to hold at least this many.
Examples
use HashMap;
let map = with_capacity;
assert!;
Inserts a key-value pair into the map.
If the map did not have this key present, [None
] is returned.
If the map did have this key present, the value is updated, and the old value is returned. The key is not updated, though; this matters for types that can be ==
without being identical. See the module-level documentation for more.
Examples
use HashMap;
let map = new;
assert_eq!;
assert_eq!;
map.insert;
assert_eq!;
assert_eq!;
Returns the value corresponding to the [Key
].
Examples
use HashMap;
let map = new;
map.insert;
assert_eq!;
assert_eq!;
Returns true
if the map contains a value for the specified [Key
].
Examples
use HashMap;
let map = new;
map.insert;
assert_eq!;
assert_eq!;
Removes a key from the map, returning the value at the [Key
] if the key was previously in the map.
Examples
use HashMap;
let map = new;
map.insert;
assert_eq!;
assert_eq!;
Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.
Examples
use HashMap;
let a = new;
a.insert;
a.clear;
assert!;
Returns true
if the map contains no elements.
Examples
use HashMap;
let a = new;
assert!;
a.insert;
assert!;
An iterator visiting all key-value pairs in arbitrary order.
Examples
use HashMap;
let map = from_iter;
let pairs = map.iter .;
pairs.sort;
assert_eq!;
Performance
In the current implementation, iterating over map takes O(capacity) time instead of O(len) because it internally visits empty buckets too.
Convert a hashmap from a value convert into an iterator.
The hashmap can be converted from anything that implements the [INTO_ITER
] protocol, and each item produces should be a tuple pair.
Examples
use HashMap;
let map = from_iter;
assert_eq!;
assert_eq!;
assert_eq!;
An iterator visiting all keys in arbitrary order.
Examples
use HashMap;
let map = from_iter;
let keys = map.keys .;
keys.sort;
assert_eq!;
Performance
In the current implementation, iterating over keys takes O(capacity) time instead of O(len) because it internally visits empty buckets too.
An iterator visiting all values in arbitrary order.
Examples
use HashMap;
let map = from_iter;
let values = map.values .;
values.sort;
assert_eq!;
Performance
In the current implementation, iterating over values takes O(capacity) time instead of O(len) because it internally visits empty buckets too.
Extend this map from an iterator.
Examples
use HashMap;
let map = new;
map.extend;
Trait Implementations
Clone the specified value
.
Examples
let a = 42;
let b = a;
let c = a.clone;
a += 1;
assert_eq!;
assert_eq!;
assert_eq!;
Compare two values for equality.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Compare two values for inequality.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Protocols
for item in value
An iterator visiting all key-value pairs in arbitrary order.
Examples
use HashMap;
let map = from_iter;
let pairs = ;
for pair in map
pairs.sort;
assert_eq!;
Performance
In the current implementation, iterating over map takes O(capacity) time instead of O(len) because it internally visits empty buckets too.
value= $input
Inserts a key-value pair into the map.
If the map did have this key present, the value is updated.
Examples
use HashMap;
let map = new;
map = "a";
assert!;
map = "c";
assert_eq!;
let $out = value
Returns a the value corresponding to the key.
Panics
Panics if the given value is not present in the map.
use HashMap;
let map = new;
let _ = map;
Examples
use HashMap;
let map = new;
map = "a";
assert_eq!;
format!
Debug format the current map.
Examples
use HashMap;
let map = new;
map = "a";
assert_eq!;
let $out = clone
Clone the map.
Examples
use HashMap;
let a = from_iter;
let b = a.clone;
b.insert;
assert_eq!;
assert_eq!;
if value == b
Perform a partial equality check over two maps.
Examples
use HashMap;
let map1 = from_iter;
let map2 = from_iter;
assert!;
map1 = f64NAN;
map2 = f64NAN;
assert!;
if value == b
Perform a total equality check over two maps.
Examples
use HashMap;
use eq;
let map1 = from_iter;
let map2 = from_iter;
assert!;