Trait DefaultMap

Source
pub trait DefaultMap: Map<V: Default + Eq> {
    // Provided method
    fn entry_or_default(
        &mut self,
        k: Self::K,
    ) -> DefaultEntry<'_, Self::VacantEntry<'_>, Self::OccupiedEntry<'_>> { ... }
}
Expand description

A mapping where non-existing keys considered to have Default values

Provided Methods§

Source

fn entry_or_default( &mut self, k: Self::K, ) -> DefaultEntry<'_, Self::VacantEntry<'_>, Self::OccupiedEntry<'_>>

Get an entry at given key or Default value if the key doesn’t exist.

The returned entry will automatically be removed from the map if it becomes equal to Default after modifications.

§Example
let mut m = HashMap::new();
*m.entry_or_default("a") += 1;
assert_eq!(m.get("a"), Some(&1));
*m.entry_or_default("a") -= 1;
assert_eq!(m.get("a"), None);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T> DefaultMap for T
where T: Map<V: Default + Eq>,