[][src]Struct easy_collections::EasyMap

pub struct EasyMap<K: Eq + Hash, V: Clone> { /* fields omitted */ }

A wrapper around HashMap that creates default values for empty keys. It also provides convenience implementations for Index and IndexMut.

For example:

use easy_collections::EasyMap;

let mut map = EasyMap::new();
assert_eq!(map['a'], 0); // default value for `usize` is `0`
// now set insert a value
map['a'] = 42_usize;
assert_eq!(map['a'], 42);

Implementations

impl<K: Eq + Hash, V: Clone + Default> EasyMap<K, V>[src]

pub fn new() -> EasyMap<K, V>[src]

Create a new EasyMap. The value V must implement Default.

Note, that there are macros which make this easier:

use easy_collections::map;

let mut map = map!{};
map[1] = (10, 4);

And another to pre-populate a map with values:

use easy_collections::map;

let map = map!{("foo", "bar"), ("hello", "world")};
assert_eq!(map["foo"], "bar");
assert_eq!(map["hello"], "world");
assert_eq!(map["not here"], "");

impl<K: Eq + Hash, V: Clone> EasyMap<K, V>[src]

pub fn new_with_default(default: V) -> EasyMap<K, V>[src]

Create a new EasyMap. The value V does not need to implement Default, instead you provide it with one here.

Note, that there's a macro which makes this easier:

use easy_collections::map;

#[derive(Debug, Clone, PartialEq)]
struct Foo(u32);

let mut map = map!{Foo(1)};
assert_eq!(map[1], Foo(1));
assert_eq!(map[2], Foo(1));
map[1] = Foo(1729);
assert_eq!(map[1], Foo(1729));

Or, the same while pre-populating the map with values:

use easy_collections::map;

let map = map!{42; ("foo", 1), ("bar", 10), ("baz", 100)};
assert_eq!(map["foo"], 1);
assert_eq!(map["bar"], 10);
assert_eq!(map["baz"], 100);
assert_eq!(map["nope"], 42);

pub fn insert(&mut self, k: K, v: V) -> Option<V>[src]

Same as HashMap::insert.

NOTE: you probably just want to use the IndexMut trait for this:

use easy_collections::EasyMap;

let mut map = EasyMap::new();
map[1] = "hello";

pub fn remove(&mut self, k: K) -> Option<V>[src]

Same as HashMap::remove.

Methods from Deref<Target = HashMap<K, V>>

pub fn capacity(&self) -> usize1.0.0[src]

Returns the number of elements the map can hold without reallocating.

This number is a lower bound; the HashMap<K, V> might be able to hold more, but is guaranteed to be able to hold at least this many.

Examples

use std::collections::HashMap;
let map: HashMap<i32, i32> = HashMap::with_capacity(100);
assert!(map.capacity() >= 100);

pub fn keys(&self) -> Keys<'_, K, V>1.0.0[src]

An iterator visiting all keys in arbitrary order. The iterator element type is &'a K.

Examples

use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for key in map.keys() {
    println!("{}", key);
}

pub fn values(&self) -> Values<'_, K, V>1.0.0[src]

An iterator visiting all values in arbitrary order. The iterator element type is &'a V.

Examples

use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for val in map.values() {
    println!("{}", val);
}

pub fn iter(&self) -> Iter<'_, K, V>1.0.0[src]

An iterator visiting all key-value pairs in arbitrary order. The iterator element type is (&'a K, &'a V).

Examples

use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for (key, val) in map.iter() {
    println!("key: {} val: {}", key, val);
}

pub fn len(&self) -> usize1.0.0[src]

Returns the number of elements in the map.

Examples

use std::collections::HashMap;

let mut a = HashMap::new();
assert_eq!(a.len(), 0);
a.insert(1, "a");
assert_eq!(a.len(), 1);

pub fn is_empty(&self) -> bool1.0.0[src]

Returns true if the map contains no elements.

Examples

use std::collections::HashMap;

let mut a = HashMap::new();
assert!(a.is_empty());
a.insert(1, "a");
assert!(!a.is_empty());

pub fn hasher(&self) -> &S1.9.0[src]

Returns a reference to the map's BuildHasher.

Examples

use std::collections::HashMap;
use std::collections::hash_map::RandomState;

let hasher = RandomState::new();
let map: HashMap<i32, i32> = HashMap::with_hasher(hasher);
let hasher: &RandomState = map.hasher();

pub fn get<Q>(&self, k: &Q) -> Option<&V> where
    K: Borrow<Q>,
    Q: Hash + Eq + ?Sized
1.0.0[src]

Returns a reference to the value corresponding to the key.

The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples

use std::collections::HashMap;

let mut map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);

pub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)> where
    K: Borrow<Q>,
    Q: Hash + Eq + ?Sized
1.40.0[src]

Returns the key-value pair corresponding to the supplied key.

The supplied key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples

use std::collections::HashMap;

let mut map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
assert_eq!(map.get_key_value(&2), None);

pub fn contains_key<Q>(&self, k: &Q) -> bool where
    K: Borrow<Q>,
    Q: Hash + Eq + ?Sized
1.0.0[src]

Returns true if the map contains a value for the specified key.

The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples

use std::collections::HashMap;

let mut map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);

pub fn raw_entry(&self) -> RawEntryBuilder<'_, K, V, S>[src]

🔬 This is a nightly-only experimental API. (hash_raw_entry)

Creates a raw immutable entry builder for the HashMap.

Raw entries provide the lowest level of control for searching and manipulating a map. They must be manually initialized with a hash and then manually searched.

This is useful for

  • Hash memoization
  • Using a search key that doesn't work with the Borrow trait
  • Using custom comparison logic without newtype wrappers

Unless you are in such a situation, higher-level and more foolproof APIs like get should be preferred.

Immutable raw entries have very limited use; you might instead want raw_entry_mut.

Trait Implementations

impl<K: Clone + Eq + Hash, V: Clone> Clone for EasyMap<K, V>[src]

impl<K: Debug + Eq + Hash, V: Debug + Clone> Debug for EasyMap<K, V>[src]

impl<K: Eq + Hash, V: Clone> Deref for EasyMap<K, V>[src]

type Target = HashMap<K, V>

The resulting type after dereferencing.

impl<K: Eq + Hash, V: Eq + Clone> Eq for EasyMap<K, V>[src]

impl<K: Eq + Hash + Clone, V: Clone + Default> From<&'_ [(K, V)]> for EasyMap<K, V>[src]

impl<K: Eq + Hash, V: Clone + Default> From<Vec<(K, V), Global>> for EasyMap<K, V>[src]

impl<K: Eq + Hash, V: Clone + Default> FromIterator<(K, V)> for EasyMap<K, V>[src]

impl<K: Eq + Hash, V: Clone> Index<K> for EasyMap<K, V>[src]

type Output = V

The returned type after indexing.

impl<K: Eq + Hash, V: Clone> IndexMut<K> for EasyMap<K, V>[src]

impl<K: Eq + Hash, V: Clone> IntoIterator for EasyMap<K, V>[src]

type Item = (K, V)

The type of the elements being iterated over.

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?

impl<K: PartialEq + Eq + Hash, V: PartialEq + Clone> PartialEq<EasyMap<K, V>> for EasyMap<K, V>[src]

impl<K: Eq + Hash, V: Clone> StructuralEq for EasyMap<K, V>[src]

impl<K: Eq + Hash, V: Clone> StructuralPartialEq for EasyMap<K, V>[src]

Auto Trait Implementations

impl<K, V> RefUnwindSafe for EasyMap<K, V> where
    K: RefUnwindSafe,
    V: RefUnwindSafe
[src]

impl<K, V> Send for EasyMap<K, V> where
    K: Send,
    V: Send
[src]

impl<K, V> Sync for EasyMap<K, V> where
    K: Sync,
    V: Sync
[src]

impl<K, V> Unpin for EasyMap<K, V> where
    K: Unpin,
    V: Unpin
[src]

impl<K, V> UnwindSafe for EasyMap<K, V> where
    K: UnwindSafe,
    V: UnwindSafe
[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.