Trait assoc::AssocStrictExt

source ·
pub trait AssocStrictExt<K, V> {
    fn entry(&mut self, key: K) -> Entry<'_, K, V>;
    fn get<Q>(&self, key: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: PartialEq + ?Sized
; fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
    where
        K: Borrow<Q>,
        Q: PartialEq + ?Sized
; fn insert(&mut self, key: K, value: V) -> Option<V>; fn remove<Q>(&mut self, key: &Q) -> Option<V>
    where
        K: Borrow<Q>,
        Q: PartialEq + ?Sized
; fn keys(&self) -> Keys<'_, K, V>; fn into_keys(self) -> IntoKeys<K, V>; fn values(&self) -> Values<'_, K, V>; fn values_mut(&mut self) -> ValuesMut<'_, K, V>; fn into_values(self) -> IntoValues<K, V>; }
Expand description

This has the same API as AssocExt but with the additional constraint K: Eq.

use assoc::AssocStrictExt;

let map = vec![(1.0, 1), (2.0, 2)];
map.entry(1.0);

Required Methods§

source

fn entry(&mut self, key: K) -> Entry<'_, K, V>

Get a key’s entry for in-place manipulation.

use assoc::AssocStrictExt;

let mut count = Vec::new();
for x in vec!["a", "b", "c", "b"] {
    *count.entry(x).or_insert(0) += 1;
}
assert_eq!(count.get(&"b"), Some(&2));
source

fn get<Q>(&self, key: &Q) -> Option<&V>where
    K: Borrow<Q>,
    Q: PartialEq + ?Sized,

Get a reference to the value associated with a key.

use assoc::AssocStrictExt;

let map = vec![("a", 1), ("b", 2)];
assert_eq!(map.get(&"a"), Some(&1));
source

fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>where
    K: Borrow<Q>,
    Q: PartialEq + ?Sized,

Get a mutable reference to the value associated with a key.

use assoc::AssocStrictExt;

let mut map = vec![("a", 1), ("b", 2)];
*map.get_mut(&"a").unwrap() += 1;
assert_eq!(map.get(&"a"), Some(&2));
source

fn insert(&mut self, key: K, value: V) -> Option<V>

Insert a key-value pair into the associative array. If the map previously had the key, then the old value is returned. Otherwise, None is returned.

use assoc::AssocStrictExt;

let mut map = vec![("b", 3)];
assert_eq!(AssocStrictExt::insert(&mut map, "a", 1), None);
assert_eq!(AssocStrictExt::insert(&mut map, "a", 2), Some(1));
source

fn remove<Q>(&mut self, key: &Q) -> Option<V>where
    K: Borrow<Q>,
    Q: PartialEq + ?Sized,

Remove a key from the map, returning the value if it was previously in the map.

use assoc::AssocStrictExt;

let mut map = vec![("a", 1)];
assert_eq!(AssocStrictExt::remove(&mut map, "a"), Some(1));
assert_eq!(AssocStrictExt::remove(&mut map, "a"), None);
source

fn keys(&self) -> Keys<'_, K, V>

source

fn into_keys(self) -> IntoKeys<K, V>

source

fn values(&self) -> Values<'_, K, V>

source

fn values_mut(&mut self) -> ValuesMut<'_, K, V>

source

fn into_values(self) -> IntoValues<K, V>

Implementations on Foreign Types§

source§

impl<K, V> AssocStrictExt<K, V> for Vec<(K, V)>where
    K: Eq,

source§

fn entry(&mut self, key: K) -> Entry<'_, K, V>

source§

fn get<Q>(&self, key: &Q) -> Option<&V>where
    K: Borrow<Q>,
    Q: PartialEq + ?Sized,

source§

fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>where
    K: Borrow<Q>,
    Q: PartialEq + ?Sized,

source§

fn insert(&mut self, key: K, value: V) -> Option<V>

source§

fn remove<Q>(&mut self, key: &Q) -> Option<V>where
    K: Borrow<Q>,
    Q: PartialEq + ?Sized,

source§

fn keys(&self) -> Keys<'_, K, V>

source§

fn into_keys(self) -> IntoKeys<K, V>

source§

fn values(&self) -> Values<'_, K, V>

source§

fn values_mut(&mut self) -> ValuesMut<'_, K, V>

source§

fn into_values(self) -> IntoValues<K, V>

Implementors§