Struct defaultmap::DefaultHashMap
source · pub struct DefaultHashMap<K: Eq + Hash, V> { /* private fields */ }
Expand description
A HashMap
that returns a default when keys are accessed that are not present.
Implementations§
source§impl<K: Eq + Hash, V: Default> DefaultHashMap<K, V>
impl<K: Eq + Hash, V: Default> DefaultHashMap<K, V>
sourcepub fn new() -> DefaultHashMap<K, V>
pub fn new() -> DefaultHashMap<K, V>
The new()
constructor creates an empty DefaultHashMap with the default of V
as the default for missing keys.
This is desired default for most use cases, if your case requires a
different default you should use the with_default()
constructor.
source§impl<K: Eq + Hash, V: Clone + 'static> DefaultHashMap<K, V>
impl<K: Eq + Hash, V: Clone + 'static> DefaultHashMap<K, V>
sourcepub fn with_default(default: V) -> DefaultHashMap<K, V>
pub fn with_default(default: V) -> DefaultHashMap<K, V>
Creates an empty DefaultHashMap
with default
as the default for missing keys.
When the provided default
is equivalent to V::default()
it is preferred to use
DefaultHashMap::default()
instead.
sourcepub fn from_map_with_default(
map: HashMap<K, V>,
default: V
) -> DefaultHashMap<K, V>
pub fn from_map_with_default( map: HashMap<K, V>, default: V ) -> DefaultHashMap<K, V>
Creates a DefaultHashMap
based on a default and an already existing HashMap
.
If V::default()
is the supplied default, usage of the from()
constructor or the
into()
method on the original HashMap
is preferred.
sourcepub fn set_default(&mut self, new_default: V)
pub fn set_default(&mut self, new_default: V)
Changes the default value permanently or until set_default()
is called again.
source§impl<K: Eq + Hash, V> DefaultHashMap<K, V>
impl<K: Eq + Hash, V> DefaultHashMap<K, V>
sourcepub fn get<Q, QB: Borrow<Q>>(&self, key: QB) -> &Vwhere
K: Borrow<Q>,
Q: ?Sized + Hash + Eq,
pub fn get<Q, QB: Borrow<Q>>(&self, key: QB) -> &Vwhere K: Borrow<Q>, Q: ?Sized + Hash + Eq,
Returns a reference to the value stored for the provided key.
If the key is not in the DefaultHashMap
a reference to the default value is returned.
Usually the map[key]
method of retrieving keys is preferred over using get
directly.
This method accepts both references and owned values as the key.
sourcepub fn get_default(&self) -> V
pub fn get_default(&self) -> V
Returns the an owned version of the default value
use defaultmap::DefaultHashMap;
assert_eq!(DefaultHashMap::<String, i32>::new().get_default(), 0);
sourcepub fn with_fn(default_fn: impl DefaultFn<V> + 'static) -> DefaultHashMap<K, V>
pub fn with_fn(default_fn: impl DefaultFn<V> + 'static) -> DefaultHashMap<K, V>
Creates an empty DefaultHashMap
with default_fn
as the default value generation
function for missing keys. When the provided default_fn
only calls clone on a value,
using DefaultHashMap::new
is preferred.
sourcepub fn from_map_with_fn(
map: HashMap<K, V>,
default_fn: impl DefaultFn<V> + 'static
) -> DefaultHashMap<K, V>
pub fn from_map_with_fn( map: HashMap<K, V>, default_fn: impl DefaultFn<V> + 'static ) -> DefaultHashMap<K, V>
Creates a DefaultHashMap
based on an existing map and using default_fn
as the default
value generation function for missing keys. When the provided default_fn
is equivalent to
V::default(), then using DefaultHashMap::from(map)
is preferred.
source§impl<K: Eq + Hash, V> DefaultHashMap<K, V>
impl<K: Eq + Hash, V> DefaultHashMap<K, V>
sourcepub fn get_mut(&mut self, key: K) -> &mut V
pub fn get_mut(&mut self, key: K) -> &mut V
Returns a mutable reference to the value stored for the provided key.
If there is no value stored for the key the default value is first inserted for this
key before returning the reference.
Usually the map[key] = new_val
is prefered over using get_mut
directly.
This method only accepts owned values as the key.
source§impl<K: Eq + Hash, V> DefaultHashMap<K, V>
impl<K: Eq + Hash, V> DefaultHashMap<K, V>
These methods simply forward to the underlying HashMap
, see that
documentation for
the usage of these methods.
pub fn capacity(&self) -> usize
pub fn keys(&self) -> Keys<'_, K, V>
pub fn into_keys(self) -> IntoKeys<K, V>
pub fn values(&self) -> Values<'_, K, V>
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V>
pub fn into_values(self) -> IntoValues<K, V>
pub fn iter(&self) -> Iter<'_, K, V>
pub fn iter_mut(&mut self) -> IterMut<'_, K, V>
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn drain(&mut self) -> Drain<'_, K, V>
pub fn retain<RF>(&mut self, f: RF)where RF: FnMut(&K, &mut V) -> bool,
pub fn clear(&mut self)
pub fn reserve(&mut self, additional: usize)
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
pub fn shrink_to_fit(&mut self)
pub fn shrink_to(&mut self, min_capacity: usize)
pub fn entry(&mut self, key: K) -> Entry<'_, K, V>
pub fn insert(&mut self, k: K, v: V) -> Option<V>
pub fn contains_key<Q>(&self, k: &Q) -> boolwhere K: Borrow<Q>, Q: ?Sized + Hash + Eq,
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>where K: Borrow<Q>, Q: ?Sized + Hash + Eq,
pub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,
Trait Implementations§
source§impl<K: Clone + Eq + Hash, V: Clone> Clone for DefaultHashMap<K, V>
impl<K: Clone + Eq + Hash, V: Clone> Clone for DefaultHashMap<K, V>
source§fn clone(&self) -> DefaultHashMap<K, V>
fn clone(&self) -> DefaultHashMap<K, V>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<K: Eq + Hash, V: Default> Default for DefaultHashMap<K, V>
impl<K: Eq + Hash, V: Default> Default for DefaultHashMap<K, V>
source§fn default() -> DefaultHashMap<K, V>
fn default() -> DefaultHashMap<K, V>
The default()
method is equivalent to DefaultHashMap::new()
.
source§impl<'de, K, V> Deserialize<'de> for DefaultHashMap<K, V>where
K: Deserialize<'de> + Eq + Hash,
V: Deserialize<'de> + Default,
impl<'de, K, V> Deserialize<'de> for DefaultHashMap<K, V>where K: Deserialize<'de> + Eq + Hash, V: Deserialize<'de> + Default,
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,
source§impl<K: Eq + Hash, V> From<DefaultHashMap<K, V>> for HashMap<K, V>
impl<K: Eq + Hash, V> From<DefaultHashMap<K, V>> for HashMap<K, V>
source§fn from(default_map: DefaultHashMap<K, V>) -> HashMap<K, V>
fn from(default_map: DefaultHashMap<K, V>) -> HashMap<K, V>
The into method can be used to convert a DefaultHashMap
back into a
HashMap
.
source§impl<K: Eq + Hash, V: Default> From<HashMap<K, V, RandomState>> for DefaultHashMap<K, V>
impl<K: Eq + Hash, V: Default> From<HashMap<K, V, RandomState>> for DefaultHashMap<K, V>
source§fn from(map: HashMap<K, V>) -> DefaultHashMap<K, V>
fn from(map: HashMap<K, V>) -> DefaultHashMap<K, V>
If you already have a HashMap
that you would like to convert to a
DefaultHashMap
you can use the into()
method on the HashMap
or the
from()
constructor of DefaultHashMap
.
The default value for missing keys will be V::default()
,
if this is not desired DefaultHashMap::from_map_with_default()
should be used.
source§impl<K: Eq + Hash, V: Default> FromIterator<(K, V)> for DefaultHashMap<K, V>
impl<K: Eq + Hash, V: Default> FromIterator<(K, V)> for DefaultHashMap<K, V>
source§impl<K: Eq + Hash, KB: Borrow<K>, V> Index<KB> for DefaultHashMap<K, V>
impl<K: Eq + Hash, KB: Borrow<K>, V> Index<KB> for DefaultHashMap<K, V>
Implements the Index
trait so you can do map[key]
.
Nonmutable indexing can be done both by passing a reference or an owned value as the key.
source§impl<K: Eq + Hash, V> IndexMut<K> for DefaultHashMap<K, V>
impl<K: Eq + Hash, V> IndexMut<K> for DefaultHashMap<K, V>
Implements the IndexMut
trait so you can do map[key] = val
.
Mutably indexing can only be done when passing an owned value as the key.