core::collections::dict
On this page
Dict<K, V, S>, a hash map
hashing with S (the process-keyed RandomState by
default), compact: entries are appended to dense key / value / hash
arrays, and a power-of-two index of linear-probed slots maps a hash to
its entry. A table of at most SMALL entries has no index: lookup
scans the hashes. remove leaves a hole; holes are squeezed out when
the entries are full. One object holds the counts and the current
arrays, swapped on growth. Iteration follows the entries, so it is in
insertion order: a present key re-inserted
keeps its place, a removed one re-inserted goes last.
Structs
DictHeadSource
pub struct DictHead<K, V, S>The dict's object. keys / vals have one more slot that stays
zero: a hole's key and value are overwritten with it, so a removed
entry keeps nothing alive. meta holds cap entry hashes (0: a
hole), then slots index slots (0: empty, else entry + 1). An index
slot of a removed entry stays, pointing at the hole: a tombstone,
passed over because no lookup hash is 0. Index slots in use never
exceed cap, at most 2/3 of slots, so probing always ends.
DictSource
pub struct Dict<K, V, S = RandomState>S builds the hashers (RandomState: the process key).
Sourcepub fn freeze(§self) -> FrozenDict<K, V, S> The dict as a
FrozenDict, which threads share without a lock or a copy. O(1) (O(n) once for a dict built at compile time): the frozen dict takes the entries over, and usingselfafter is a compile error (to_dict()gives a dict to change again; freeze aclone()to keep this one).An empty dict; its storage is allocated at the first insert.
Room for
capentries before the first growth.An empty dict hashing with
state's hashers; its storage is allocated at the first insert.Room for
capentries, hashing withstate's hashers.What builds the dict's hashers.
The number of entries.
truewhen the dict has no entry.Insert or replace; the old value, or
none.The value under
k;nonewhenkis absent.The value under
k;defaultwhenkis absent. The dict is not changed. Python:d.get(k, default).The value under
k, first insertingdefaultwhenkis absent.d[k, default: v]as an assignment target uses it.truewhenkhas an entry.Remove
k's entry and return its value;nonewhenkis absent.Remove
k; the stored key and its value, ornonewhen absent.Remove and return the entry inserted last, or
nonewhen the dict is empty. Python:d.popitem().nabilet d = {"a": 1, "b": 2, "c": 3}; let (k, v) = d.pop_last() ?? ("", 0); assert(k == "c" && v == 3); let (k1, _) = d.pop_first() ?? ("", 0); let (k2, _) = d.remove_entry("b") ?? ("", 0); assert(k1 == "a" && k2 == "b" && d.is_empty() && d.pop_last() is none);Remove and return the entry inserted first, or
nonewhen the dict is empty.Remove every entry and return them, in insertion order.
nabilet d = {"x": 1, "y": 2}; assert(d.drain() == [("x", 1), ("y", 2)] && d.is_empty());Keep only the entries
keepaccepts, in place; O(n).
Sourcepub const fn extend<I: IntoIterator<Item = (K, V)>>(§self, pairs: I) Insert every
(key, value)pair ofpairs, in order; a later pair's value wins.Drop every entry (a fresh empty table, allocated at the next insert).
The keys, lazily, in insertion order.
The values, lazily.
(key, value)pairs, lazily (insertion order).k's place in the dict, present or not.The
nlargest counts (all whennisnone) as(key, count), largest first; equal counts keep first-seen order. Python:Counter.most_common(n); build the counts withxs.counts().
Trait implementations 14
impl<K, V, S> !Sync for Dict<K, V, S>impl<K: Hash, V, S: BuildHasher + Default> FromIterator<(K, V)> for Dict<K, V, S>impl<K: Hash, V, S: BuildHasher + Default> Default for Dict<K, V, S>impl<K: Hash + Clone, V: Clone, S: BuildHasher> Clone for Dict<K, V, S>impl<K: Hash, V, S: BuildHasher + Default> From<List<(K, V)>> for Dict<K, V, S>impl<K: Hash, V, S: BuildHasher> AddAssign<Dict<K, V, S>> for Dict<K, V, S>impl<K: Hash, V, S: BuildHasher> IntoIterator for Dict<K, V, S>impl<K: Hash, V, S: BuildHasher> Index<K> for Dict<K, V, S>impl<K: Hash, V, S: BuildHasher> IndexSet<K, V> for Dict<K, V, S>impl<K: Hash, V: PartialEq, S: BuildHasher> PartialEq for Dict<K, V, S>impl<K: Hash, V: Eq, S: BuildHasher> Eq for Dict<K, V, S>impl<K: Hash, V: Hash, S: BuildHasher> Hash for Dict<K, V, S>impl<K: Hash + Debug, V: Debug, S: BuildHasher> Debug for Dict<K, V, S>impl<K, V, S> Transfer for Dict<K, V, S>
EntrySource
pub struct Entry<K, V, S = RandomState>d.entry(k): read or fill one key's slot. Each call looks the key
up again.
The entry's key.
The value, after inserting
vif the key is absent.The value, after inserting
f()if the key is absent.A present value replaced by
f(value); an absent key is left alone.The value, after inserting
V::default()if the key is absent.
Trait implementations 1
impl<K, V, S> Debug for Entry<K, V, S>
KeysSource
pub struct Keys<K, V, S = RandomState>What d.keys() returns: the keys, in insertion order. Changing the
dict while iterating gives unspecified (memory-safe) results.
Trait implementations 2
impl<K, V, S> Debug for Keys<K, V, S>impl<K: Hash, V, S: BuildHasher> Iterator for Keys<K, V, S>
ValuesSource
pub struct Values<K, V, S = RandomState>What d.values() returns: the values, in insertion order. Changing
the dict while iterating gives unspecified (memory-safe) results.
Trait implementations 2
impl<K, V, S> Debug for Values<K, V, S>impl<K: Hash, V, S: BuildHasher> Iterator for Values<K, V, S>
DictIterSource
pub struct DictIter<K, V, S = RandomState>What d.iter() returns: (key, value) pairs, in insertion order.
Changing the dict while iterating gives unspecified (memory-safe)
results.
Trait implementations 2
impl<K, V, S> Debug for DictIter<K, V, S>impl<K: Hash, V, S: BuildHasher> Iterator for DictIter<K, V, S>