Struct HashMap

Source
pub struct HashMap<K: Hash + Eq, V, B: BuildHasher = RandomState> { /* private fields */ }
Expand description

A map of keys to values which relies on the keys implementing Hash.

A custom load factor is not supported at this point, with the default being 4/5.

It is a logic error for keys in a HashMap to be manipulated in a way that changes their hash. Because of this, HashMap’s API prevents mutable access to its keys.

§Time Complexity

For this analysis of time complexity, variables are defined as follows:

  • n: The number of items in the HashMap.
MethodComplexity
lenO(1)
insertO(1)**, O(n)
insert_uncheckedO(1)*
getO(1)*
removeO(1)*
containsO(1)*
reserveO(n)***, O(1)

* In the event of a has collision, these functions will take additional time, while a valid / correct location is found. This additional time is kept at a minimum and hash collisions are unlikely especially with a large capacity.

** If the HashMap doesn’t have enough capacity for the new element, insert will take O(n). * applies as well.

*** If the HashMap has enough capacity for the additional items already, reserve is O(1).

Implementations§

Source§

impl<K: Hash + Eq, V, B: BuildHasher + Default> HashMap<K, V, B>

Source

pub fn new() -> HashMap<K, V, B>

Creates a new HashMap with capacity 0 and the default value for B. Memory will be allocated when the capacity changes.

Source

pub fn with_cap(cap: usize) -> HashMap<K, V, B>

Creates a new HashMap with the provided capacity, allowing insertions without reallocation. The default hasher will be used.

Source§

impl<K: Hash + Eq, V, B: BuildHasher> HashMap<K, V, B>

Source

pub fn with_hasher(hasher: B) -> HashMap<K, V, B>

Creates a new HashMap with capacity 0 and the provided hasher.

Source

pub fn with_cap_and_hasher(cap: usize, hasher: B) -> HashMap<K, V, B>

Creates a new HashMap with the provided capacity and hasher.

Source

pub const fn len(&self) -> usize

Returns the length of the HashMap (the number of entries it contains).

Source

pub const fn is_empty(&self) -> bool

Returns true if the HashMap contains no entries.

Source

pub const fn cap(&self) -> usize

Returns the current capacity of the HashMap.

Source

pub fn insert(&mut self, key: K, value: V) -> Option<V>

Inserts the provided key-value pair into the HashMap, increasing its capacity if required. If the key was already associated with a value, the previous value is returned.

As with the standard library, the key isn’t changed if it already exists.

Source

pub unsafe fn insert_unchecked(&mut self, key: K, value: V) -> Option<V>

Inserts the provided key-value pair without checking if the HashMap has enough capacity. If the key was already associated with a value, the previous value is returned.

As with the standard library, the key isn’t changed if it already exists.

§Safety

It is the responsibility of the caller to ensure that the HashMap has enough capacity to add the provided entry, using methods like reserve or with_cap.

§Panics

Panics if the HashMap has a capacity of 0, as it isn’t possible to find a bucket associated with the key.

Source

pub fn get_entry<Q>(&self, key: &Q) -> Option<(&K, &V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns the entry for the provided key as a key-value pair or None if there is no entry.

Source

pub fn get<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a reference to the value associated with the provided key or None if the map contains no values for key.

Source

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a mutable reference to the value associated with the provided key or None if the map contains no values for key.

Source

pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes the entry associated with key, returning it if it exists.

Source

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes the entry associated with key, returning the value if it exists.

Source

pub fn contains<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns true if there is a value associated with the provided key.

Source

pub fn reserve(&mut self, extra: usize)

Increases the capacity of the HashMap to ensure that len + extra entries will fit without exceeding the load factor.

Source

pub fn iter(&self) -> Iter<'_, K, V>

Returns an iterator over all key-value pairs in the HashMap, as references.

Source

pub fn into_keys(self) -> IntoKeys<K, V>

Consumes self and returns an iterator over all contained keys.

Source

pub fn keys<'a>(&'a self) -> Keys<'a, K, V>

Returns an iterator over all keys in the HashMap, as references.

Source

pub fn into_values(self) -> IntoValues<K, V>

Consumes self and returns an iterator over all contained values.

Source

pub fn values_mut<'a>(&'a mut self) -> ValuesMut<'a, K, V>

Returns an iterator over all values in the HashMap, as mutable references.

Source

pub fn values<'a>(&'a self) -> Values<'a, K, V>

Returns an iterator over all values in the HashMap, as references.

Trait Implementations§

Source§

impl<K: Hash + Eq + Debug, V: Debug, B: BuildHasher + Debug> Debug for HashMap<K, V, B>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: Hash + Eq, V, B: BuildHasher + Default> Default for HashMap<K, V, B>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<K: Hash + Eq + Debug, V: Debug, B: BuildHasher> Display for HashMap<K, V, B>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: Hash + Eq, V, B: BuildHasher + Default> Extend<(K, V)> for HashMap<K, V, B>

Source§

fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: (K, V))

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K, V, B, I> From<I> for HashMap<K, V, B>

Source§

fn from(value: I) -> Self

Converts to this type from the input type.
Source§

impl<K: Hash + Eq, V, B: BuildHasher + Default> FromIterator<(K, V)> for HashMap<K, V, B>

Source§

fn from_iter<I: IntoIterator<Item = (K, V)>>(value: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<Q, K, V> Index<&Q> for HashMap<K, V>
where Q: Hash + Eq + ?Sized, K: Hash + Eq + Borrow<Q>,

Source§

type Output = V

The returned type after indexing.
Source§

fn index(&self, key: &Q) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, K: Hash + Eq, V, B: BuildHasher> IntoIterator for &'a HashMap<K, V, B>

Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K: Hash + Eq, V, B: BuildHasher> IntoIterator for HashMap<K, V, B>

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<K, V, B> Freeze for HashMap<K, V, B>
where B: Freeze,

§

impl<K, V, B> RefUnwindSafe for HashMap<K, V, B>

§

impl<K, V, B> Send for HashMap<K, V, B>
where B: Send, K: Send, V: Send,

§

impl<K, V, B> Sync for HashMap<K, V, B>
where B: Sync, K: Sync, V: Sync,

§

impl<K, V, B> Unpin for HashMap<K, V, B>
where B: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, B> UnwindSafe for HashMap<K, V, B>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.