pub struct HashMap<K, V, S = FxBuildHasher>(/* private fields */);Expand description
A hash map implemented with quadratic probing.
Implementations§
Source§impl<K, V> HashMap<K, V, FxBuildHasher>
impl<K, V> HashMap<K, V, FxBuildHasher>
Sourcepub fn new() -> HashMap<K, V, FxBuildHasher>
pub fn new() -> HashMap<K, V, FxBuildHasher>
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.
Source§impl<K, V, S> HashMap<K, V, S>
impl<K, V, S> HashMap<K, V, S>
pub fn from_std(hash_map: StdHashMap<K, V, S>) -> Self
pub fn into_std(self) -> StdHashMap<K, V, S>
Sourcepub fn with_hasher(hash_builder: S) -> HashMap<K, V, S>
pub fn with_hasher(hash_builder: S) -> HashMap<K, V, S>
Creates an empty HashMap which will use the given hash builder to hash
keys.
The created map has the default initial capacity.
Warning: hash_builder is normally randomly generated, and
is designed to allow HashMaps to be resistant to attacks that
cause many collisions and very poor performance. Setting it
manually using this function can expose a DoS attack vector.
The hash_builder passed should implement the BuildHasher trait for
the HashMap to be useful, see its documentation for details.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
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.
Sourcepub fn keys(&self) -> Keys<'_, K, V> ⓘ
pub fn keys(&self) -> Keys<'_, K, V> ⓘ
An iterator visiting all keys in arbitrary order.
The iterator element type is &'a K.
Sourcepub fn into_keys(self) -> IntoKeys<K, V> ⓘ
pub fn into_keys(self) -> IntoKeys<K, V> ⓘ
Creates a consuming iterator visiting all the keys in arbitrary order.
The map cannot be used after calling this.
The iterator element type is K.
Sourcepub fn values(&self) -> Values<'_, K, V> ⓘ
pub fn values(&self) -> Values<'_, K, V> ⓘ
An iterator visiting all values in arbitrary order.
The iterator element type is &'a V.
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
An iterator visiting all values mutably in arbitrary order.
The iterator element type is &'a mut V.
Sourcepub fn into_values(self) -> IntoValues<K, V> ⓘ
pub fn into_values(self) -> IntoValues<K, V> ⓘ
Creates a consuming iterator visiting all the values in arbitrary order.
The map cannot be used after calling this.
The iterator element type is V.
Sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, K, V> ⓘ
An iterator visiting all key-value pairs in arbitrary order.
The iterator element type is (&'a K, &'a V).
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
An iterator visiting all key-value pairs in arbitrary order,
with mutable references to the values.
The iterator element type is (&'a K, &'a mut V).
Sourcepub fn drain(&mut self) -> Drain<'_, K, V> ⓘ
pub fn drain(&mut self) -> Drain<'_, K, V> ⓘ
Clears the map, returning all key-value pairs as an iterator. Keeps the allocated memory for reuse.
If the returned iterator is dropped before being fully consumed, it drops the remaining key-value pairs. The returned iterator keeps a mutable borrow on the vector to optimize its implementation.
Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Retains only the elements specified by the predicate.
In other words, remove all pairs (k, v) for which f(&k, &mut v) returns false.
The elements are visited in unsorted (and unspecified) order.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.
Sourcepub fn hasher(&self) -> &S
pub fn hasher(&self) -> &S
Returns a reference to the map’s BuildHasher.
Source§impl<K, V, S> HashMap<K, V, S>
impl<K, V, S> HashMap<K, V, S>
Sourcepub fn try_with_capacity(
capacity: usize,
) -> Result<HashMap<K, V, S>, AllocError>where
S: Default,
pub fn try_with_capacity(
capacity: usize,
) -> Result<HashMap<K, V, S>, AllocError>where
S: Default,
Creates an empty HashMap with the specified capacity.
The hash map will be able to hold at least capacity elements without
reallocating. If capacity is 0, the hash map will not allocate.
Sourcepub fn try_with_capacity_and_hasher(
capacity: usize,
hash_builder: S,
) -> Result<HashMap<K, V, S>, AllocError>
pub fn try_with_capacity_and_hasher( capacity: usize, hash_builder: S, ) -> Result<HashMap<K, V, S>, AllocError>
Creates an empty HashMap with the specified capacity, using hash_builder
to hash the keys.
The hash map will be able to hold at least capacity elements without
reallocating. If capacity is 0, the hash map will not allocate.
Warning: hash_builder is normally randomly generated, and
is designed to allow HashMaps to be resistant to attacks that
cause many collisions and very poor performance. Setting it
manually using this function can expose a DoS attack vector.
The hash_builder passed should implement the BuildHasher trait for
the HashMap to be useful, see its documentation for details.
Sourcepub fn try_reserve(&mut self, additional: usize) -> Result<(), AllocError>
pub fn try_reserve(&mut self, additional: usize) -> Result<(), AllocError>
Tries to reserve capacity for at least additional more elements to be inserted
in the given HashMap<K, V>. The collection may reserve more space to avoid
frequent reallocations.
§Errors
If the capacity overflows, or the allocator reports a failure, then an error is returned.
Sourcepub fn try_entry(&mut self, key: K) -> Result<Entry<'_, K, V>, AllocError>
pub fn try_entry(&mut self, key: K) -> Result<Entry<'_, K, V>, AllocError>
Gets the given key’s corresponding entry in the map for in-place manipulation.
Sourcepub fn contains_key<Q>(&self, k: &Q) -> bool
pub fn contains_key<Q>(&self, k: &Q) -> bool
Sourcepub fn try_insert(&mut self, k: K, v: V) -> Result<Option<V>, AllocError>
pub fn try_insert(&mut self, k: K, v: V) -> Result<Option<V>, AllocError>
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.