pub struct DefaultHashMap<K, V> where
    K: Eq + Hash,
    V: Default
{ /* private fields */ }
Expand description

This struct mimicks the behaviour of a python defaultdict. This means alongside the traitbounds that apply on the key and value that are inherited from the HashMap, it also requires the Default trait be implemented on the value type.

Implementations

Creates an empty DefaultHashMap.

Example
use defaultdict::DefaultHashMap;

let map = DefaultHashMap::<i8, i8>::new();

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));

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));

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));

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)

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());

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());

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());

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));

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);

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

Formats the value using the given formatter. Read more

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

Converts to this type from the input type.

The returned type after indexing.

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

Converts this type into the (usually inferred) input type.

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.