Struct defaultdict::DefaultHashMap
source · [−]Expand description
Implementations
sourceimpl<K, V> DefaultHashMap<K, V> where
K: Eq + Hash,
V: Default,
impl<K, V> DefaultHashMap<K, V> where
K: Eq + Hash,
V: Default,
sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty DefaultHashMap.
Example
use defaultdict::DefaultHashMap;
let map = DefaultHashMap::<i8, i8>::new();sourcepub fn contains_key(&self, key: &K) -> bool where
K: Eq + Hash,
pub fn contains_key(&self, key: &K) -> bool where
K: Eq + Hash,
Returns true if the key passed in exists in the HashMap.
Example
use defaultdict::DefaultHashMap;
let mut map = DefaultHashMap::<i8, i8>::new();
map.insert(10, 20);
println!("{}", map.contains_key(&10));sourcepub fn get(&self, key: &K) -> &V where
K: Eq + Hash + Clone,
pub fn get(&self, key: &K) -> &V where
K: Eq + Hash + Clone,
Returns a reference to the value of the key passed in. Because this hashmap mimicks the python defaultdict, it will also return a reference to a value if the key is not present.
The key type must implement Hash and Eq.
Example
use defaultdict::DefaultHashMap;
let mut map = DefaultHashMap::<i8, i8>::new();
map.insert(10, 20);
println!("{}", map.get(&10));sourcepub fn get_mut(&mut self, key: &K) -> &mut V where
K: Hash + Eq + Clone,
pub fn get_mut(&mut self, key: &K) -> &mut V where
K: Hash + Eq + Clone,
Returns a mutable reference to the value corresponding to the key. If the key is not present in the hashmap it will return the default value and insert it in the map.
Example
use defaultdict::DefaultHashMap;
let mut map = DefaultHashMap::<i8, i8>::new();
let number = map.get_mut(&10);
*number = 100;
println!("{}", map.get(&10));sourcepub fn insert(&mut self, key: K, value: V) where
K: Eq + Hash,
pub fn insert(&mut self, key: K, value: V) where
K: Eq + Hash,
Inserts a key value pair into the map.
If the map had the key already present it will be overwritten.
Example
use defaultdict::DefaultHashMap;
let mut map = DefaultHashMap::<i8, i8>::new();
map.insert(10, 20)sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the map does not contain any keys.
Example
use defaultdict::DefaultHashMap;
let mut map = DefaultHashMap::<i8, i8>::new();
println!("{}", map.is_empty());sourcepub fn keys(&self) -> Keys<'_, K, V>
pub fn keys(&self) -> Keys<'_, K, V>
Returns an iterator visiting all keys in arbitrary order. The iterator element type is &’a K.
Example
use defaultdict::DefaultHashMap;
let mut map = DefaultHashMap::<i8, i8>::new();
map.insert(10, 20);
map.insert(11, 21);
map.insert(12, 22);
map.insert(13, 23);
println!("{:?}", map.keys());sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the keys in the map.
Example
use defaultdict::DefaultHashMap;
let mut map = DefaultHashMap::<i8, i8>::new();
map.insert(10, 20);
map.insert(11, 21);
map.insert(12, 22);
map.insert(13, 23);
println!("{}", map.len());sourcepub fn remove(&mut self, key: &K) -> V where
K: Hash + Eq,
pub fn remove(&mut self, key: &K) -> V where
K: Hash + Eq,
Removes a key from the map, returning the value at the key if the key was previously in the map. If the key is not present in the map it will return the default value.
Example
use defaultdict::DefaultHashMap;
let mut map = DefaultHashMap::<i8, i8>::new();
map.insert(10, 20);
println!("{}", map.remove(&10));
println!("{}", map.remove(&90));sourcepub fn retain<F>(&mut self, func: F) where
F: FnMut(&K, &mut V) -> bool,
pub fn retain<F>(&mut self, func: F) where
F: FnMut(&K, &mut V) -> bool,
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.
Example
use defaultdict::{DefaultHashMap, defaulthashmap};
let mut map = defaulthashmap!(
(0, 0),
(1, 0),
(2, 0),
(3, 0),
(4, 0),
(5, 0),
(6, 0),
(7, 0),
(8, 0),
(9, 0),
);
map.retain(|key, value| {
key <= &2
});
println!("{:?}", map);sourcepub fn values(&self) -> Values<'_, K, V>
pub fn values(&self) -> Values<'_, K, V>
Returns an iterator visiting all values in arbitrary order. The iterator element type is &’a V.
Example
use defaultdict::DefaultHashMap;
let mut map = DefaultHashMap::<i8, i8>::new();
map.insert(10, 20);
map.insert(11, 21);
map.insert(12, 22);
map.insert(13, 23);
println!("{:?}", map.values());Trait Implementations
sourceimpl<K, V> From<HashMap<K, V, RandomState>> for DefaultHashMap<K, V> where
K: Eq + Hash + Ord + Clone,
V: Default,
impl<K, V> From<HashMap<K, V, RandomState>> for DefaultHashMap<K, V> where
K: Eq + Hash + Ord + Clone,
V: Default,
sourceimpl<K, V> Into<HashMap<K, V, RandomState>> for DefaultHashMap<K, V> where
K: Eq + Hash + Ord + Clone,
V: Default,
impl<K, V> Into<HashMap<K, V, RandomState>> for DefaultHashMap<K, V> where
K: Eq + Hash + Ord + Clone,
V: Default,
sourceimpl<'a, K, V> IntoIterator for &'a DefaultHashMap<K, V> where
K: Eq + Hash + Ord + Clone,
V: Default,
impl<'a, K, V> IntoIterator for &'a DefaultHashMap<K, V> where
K: Eq + Hash + Ord + Clone,
V: Default,
sourceimpl<'a, K, V> IntoIterator for &'a mut DefaultHashMap<K, V> where
K: Eq + Hash + Ord + Clone,
V: Default,
impl<'a, K, V> IntoIterator for &'a mut DefaultHashMap<K, V> where
K: Eq + Hash + Ord + Clone,
V: Default,
sourceimpl<K, V> IntoIterator for DefaultHashMap<K, V> where
K: Eq + Hash + Ord + Clone,
V: Default,
impl<K, V> IntoIterator for DefaultHashMap<K, V> where
K: Eq + Hash + Ord + Clone,
V: Default,
sourceimpl<K: PartialEq, V: PartialEq> PartialEq<DefaultHashMap<K, V>> for DefaultHashMap<K, V> where
K: Eq + Hash,
V: Default,
impl<K: PartialEq, V: PartialEq> PartialEq<DefaultHashMap<K, V>> for DefaultHashMap<K, V> where
K: Eq + Hash,
V: Default,
sourcefn eq(&self, other: &DefaultHashMap<K, V>) -> bool
fn eq(&self, other: &DefaultHashMap<K, V>) -> bool
This method tests for self and other values to be equal, and is used
by ==. Read more
impl<K: Eq, V: Eq> Eq for DefaultHashMap<K, V> where
K: Eq + Hash,
V: Default,
impl<K, V> StructuralEq for DefaultHashMap<K, V> where
K: Eq + Hash,
V: Default,
impl<K, V> StructuralPartialEq for DefaultHashMap<K, V> where
K: Eq + Hash,
V: Default,
Auto Trait Implementations
impl<K, V> RefUnwindSafe for DefaultHashMap<K, V> where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for DefaultHashMap<K, V> where
K: Send,
V: Send,
impl<K, V> Sync for DefaultHashMap<K, V> where
K: Sync,
V: Sync,
impl<K, V> Unpin for DefaultHashMap<K, V> where
K: Unpin,
V: Unpin,
impl<K, V> UnwindSafe for DefaultHashMap<K, V> where
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more