Trait assoc::AssocExt

source ·
pub trait AssocExt<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

A trait extension that allows vectors to be treated as associative arrays.

Required Methods§

source

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

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

use assoc::AssocExt;

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::AssocExt;

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::AssocExt;

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::AssocExt;

let mut map = vec![("b", 3)];
assert_eq!(AssocExt::insert(&mut map, "a", 1), None);
assert_eq!(AssocExt::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::AssocExt;

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

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

Get an iterator over the keys of the map.

use assoc::AssocExt;

let map = vec![("a", 1), ("b", 2)];
let mut iter = map.keys();

assert_eq!(iter.next().unwrap(), &"a");
assert_eq!(iter.next().unwrap(), &"b");
assert_eq!(iter.next(), None);
source

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

Create a consuming iterator visiting all the keys of the map.

use assoc::AssocExt;

let map = vec![(1, "a"), (2, "b")];
let keys: Vec<i32> = map.into_keys().collect();
assert_eq!(keys, [1, 2]);
source

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

Get an iterator over the values of the map.

use assoc::AssocExt;

let map = vec![("a", 1), ("b", 2)];
let mut iter = map.values();

assert_eq!(iter.next().unwrap(), &1);
assert_eq!(iter.next().unwrap(), &2);
assert_eq!(iter.next(), None);
source

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

Get a mutable iterator over the values of the map.

use assoc::AssocExt;

let mut map = vec![(1, "a".to_string()), (2, "b".to_string())];

for value in map.values_mut() {
    value.push_str("!");
}

let values: Vec<String> = map.values().cloned().collect();
assert_eq!(values, ["a!".to_string(), "b!".to_string()]);
source

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

Create a consuming iterator visiting all the values of the map.

use assoc::AssocExt;

let map = vec![(1, "a"), (2, "b")];
let values: Vec<&str> = map.into_values().collect();
assert_eq!(values, ["a", "b"]);

Implementations on Foreign Types§

source§

impl<K, V> AssocExt<K, V> for Vec<(K, V)>where
    K: PartialEq,

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§