Struct vecmap::VecMap

source ·
pub struct VecMap<K, V> { /* private fields */ }
Expand description

A vector-based map implementation which retains the order of inserted entries.

Internally it is represented as a Vec<(K, V)> to support keys that are neither Hash nor Ord.

Implementations§

source§

impl<K, V> VecMap<K, V>

source

pub const fn new() -> Self

Create a new map. (Does not allocate.)

Examples
use vecmap::VecMap;

let mut map: VecMap<i32, &str> = VecMap::new();
source

pub fn with_capacity(capacity: usize) -> Self

Create a new map with capacity for capacity key-value pairs. (Does not allocate if capacity is zero.)

Examples
use vecmap::VecMap;

let mut map: VecMap<i32, &str> = VecMap::with_capacity(10);
assert_eq!(map.len(), 0);
assert!(map.capacity() >= 10);
source

pub fn capacity(&self) -> usize

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

Examples
use vecmap::VecMap;

let mut map: VecMap<i32, &str> = VecMap::with_capacity(10);
assert_eq!(map.capacity(), 10);
source

pub fn len(&self) -> usize

Returns the number of entries in the map, also referred to as its ‘length’.

Examples
use vecmap::VecMap;

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

pub fn is_empty(&self) -> bool

Returns true if the map contains no entries.

Examples
use vecmap::VecMap;

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

pub fn clear(&mut self)

Clears the map, removing all entries.

Examples
use vecmap::VecMap;

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

pub fn truncate(&mut self, len: usize)

Shortens the map, keeping the first len key-value pairs and dropping the rest.

If len is greater than the map’s current length, this has no effect.

Examples

Truncating a four element map to two elements:

use vecmap::VecMap;

let mut map = VecMap::from([("a", 1), ("b", 2), ("c", 3), ("d", 4)]);
map.truncate(2);
assert_eq!(map, VecMap::from([("a", 1), ("b", 2)]));

No truncation occurs when len is greater than the map’s current length:

use vecmap::VecMap;

let mut map = VecMap::from([("a", 1), ("b", 2), ("c", 3), ("d", 4)]);
map.truncate(8);
assert_eq!(map, VecMap::from([("a", 1), ("b", 2), ("c", 3), ("d", 4)]));
source

pub fn reverse(&mut self)

Reverses the order of entries in the map, in place.

Examples
use vecmap::VecMap;

let mut map = VecMap::from_iter([("a", 1), ("b", 2), ("c", 3)]);
map.reverse();
let reversed: Vec<(&str, u8)> = map.into_iter().collect();
assert_eq!(reversed, Vec::from_iter([("c", 3), ("b", 2), ("a", 1)]));
source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements to be inserted in the given VecMap<K, V>. The collection may reserve more space to speculatively avoid frequent reallocations. After calling reserve, capacity will be greater than or equal to self.len() + additional. Does nothing if capacity is already sufficient.

Panics

Panics if the new capacity exceeds isize::MAX bytes.

Examples
use vecmap::VecMap;

let mut map = VecMap::from_iter([("a", 1)]);
map.reserve(10);
assert!(map.capacity() >= 11);
source

pub fn retain<F>(&mut self, f: 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.

Examples
use vecmap::VecMap;

let mut map: VecMap<i32, i32> = (0..8).map(|x| (x, x*10)).collect();
map.retain(|&k, _| k % 2 == 0);
assert_eq!(map.len(), 4);
source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the map as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.

Examples
use vecmap::VecMap;

let mut map: VecMap<i32, i32> = VecMap::with_capacity(100);
map.insert(1, 2);
map.insert(3, 4);
assert!(map.capacity() >= 100);
map.shrink_to_fit();
assert!(map.capacity() >= 2);
source

pub fn shrink_to(&mut self, min_capacity: usize)

Shrinks the capacity of the map with a lower limit. It will drop down no lower than the supplied limit while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.

If the current capacity is less than the lower limit, this is a no-op.

Examples
use vecmap::VecMap;

let mut map: VecMap<i32, i32> = VecMap::with_capacity(100);
map.insert(1, 2);
map.insert(3, 4);
assert!(map.capacity() >= 100);
map.shrink_to(10);
assert!(map.capacity() >= 10);
map.shrink_to(0);
assert!(map.capacity() >= 2);
source

pub fn split_off(&mut self, at: usize) -> VecMap<K, V>

Splits the map into two at the given index.

Returns a newly allocated map containing the key-value pairs in the range [at, len). After the call, the original map will be left containing the key-value pairs [0, at) with its previous capacity unchanged.

Panics

Panics if at > len.

Examples
use vecmap::VecMap;

let mut map = VecMap::from([("a", 1), ("b", 2), ("c", 3)]);
let map2 = map.split_off(1);
assert_eq!(map, VecMap::from([("a", 1)]));
assert_eq!(map2, VecMap::from([("b", 2), ("c", 3)]));
source

pub fn drain<R>(&mut self, range: R) -> Drain<'_, K, V>
where R: RangeBounds<usize>,

Removes the specified range from the vector in bulk, returning all removed elements as an iterator. If the iterator is dropped before being fully consumed, it drops the remaining removed elements.

The returned iterator keeps a mutable borrow on the vector to optimize its implementation.

Panics

Panics if the starting point is greater than the end point or if the end point is greater than the length of the vector.

Examples
use vecmap::{vecmap, VecMap};

let mut v = vecmap!["a" => 1, "b" => 2, "c" => 3];
let u: VecMap<_, _> = v.drain(1..).collect();
assert_eq!(v, vecmap!["a" => 1]);
assert_eq!(u, vecmap!["b" => 2, "c" => 3]);

// A full range clears the vector, like `clear()` does
v.drain(..);
assert_eq!(v, vecmap![]);
source

pub fn sort_keys(&mut self)
where K: Ord,

Sorts the map by key.

This sort is stable (i.e., does not reorder equal elements) and O(n * log(n)) worst-case.

When applicable, unstable sorting is preferred because it is generally faster than stable sorting and it doesn’t allocate auxiliary memory. See sort_unstable_keys.

Examples
use vecmap::VecMap;

let mut map = VecMap::from([("b", 2), ("a", 1), ("c", 3)]);

map.sort_keys();
let vec: Vec<_> = map.into_iter().collect();
assert_eq!(vec, [("a", 1), ("b", 2), ("c", 3)]);
source

pub fn sort_unstable_keys(&mut self)
where K: Ord,

Sorts the map by key.

This sort is unstable (i.e., may reorder equal elements), in-place (i.e., does not allocate), and O(n * log(n)) worst-case.

Examples
use vecmap::VecMap;

let mut map = VecMap::from([("b", 2), ("a", 1), ("c", 3)]);

map.sort_unstable_keys();
let vec: Vec<_> = map.into_iter().collect();
assert_eq!(vec, [("a", 1), ("b", 2), ("c", 3)]);
source

pub fn sort_by<F>(&mut self, compare: F)
where F: FnMut((&K, &V), (&K, &V)) -> Ordering,

Sorts the map with a comparator function.

This sort is stable (i.e., does not reorder equal elements) and O(n * log(n)) worst-case.

Examples
use vecmap::VecMap;

let mut map = VecMap::from([("b", 2), ("a", 1), ("c", 3)]);

map.sort_by(|(k1, _), (k2, _)| k2.cmp(&k1));
let vec: Vec<_> = map.into_iter().collect();
assert_eq!(vec, [("c", 3), ("b", 2), ("a", 1)]);
source

pub fn sort_unstable_by<F>(&mut self, compare: F)
where F: FnMut((&K, &V), (&K, &V)) -> Ordering,

Sorts the map with a comparator function.

This sort is unstable (i.e., may reorder equal elements), in-place (i.e., does not allocate), and O(n * log(n)) worst-case.

Examples
use vecmap::VecMap;

let mut map = VecMap::from([("b", 2), ("a", 1), ("c", 3)]);

map.sort_unstable_by(|(k1, _), (k2, _)| k2.cmp(&k1));
let vec: Vec<_> = map.into_iter().collect();
assert_eq!(vec, [("c", 3), ("b", 2), ("a", 1)]);
source

pub fn as_slice(&self) -> &[(K, V)]

Extracts a slice containing the map entries.

use vecmap::VecMap;

let map = VecMap::from([("b", 2), ("a", 1), ("c", 3)]);
let slice = map.as_slice();
assert_eq!(slice, [("b", 2), ("a", 1), ("c", 3)]);
source

pub fn to_vec(&self) -> Vec<(K, V)>
where K: Clone, V: Clone,

Copies the map entries into a new Vec<(K, V)>.

use vecmap::VecMap;

let map = VecMap::from([("b", 2), ("a", 1), ("c", 3)]);
let vec = map.to_vec();
assert_eq!(vec, [("b", 2), ("a", 1), ("c", 3)]);
// Here, `map` and `vec` can be modified independently.
source

pub fn into_vec(self) -> Vec<(K, V)>

Takes ownership of the map and returns its entries as a Vec<(K, V)>.

use vecmap::VecMap;

let map = VecMap::from([("b", 2), ("a", 1), ("c", 3)]);
let vec = map.into_vec();
assert_eq!(vec, [("b", 2), ("a", 1), ("c", 3)]);
source

pub unsafe fn from_vec_unchecked(vec: Vec<(K, V)>) -> Self

Takes ownership of provided vector and converts it into VecMap.

Safety

The vector must have no duplicate keys. One way to guarantee it is to sort the vector (e.g. by using [T]::sort_by_key) and then drop duplicate keys (e.g. by using Vec::dedup_by_key).

Example
use vecmap::VecMap;

let mut vec = vec![("b", 2), ("a", 1), ("c", 3), ("b", 4)];
vec.sort_by_key(|slot| slot.0);
vec.dedup_by_key(|slot| slot.0);
// SAFETY: We've just deduplicated the vector.
let map = unsafe { VecMap::from_vec_unchecked(vec) };

assert_eq!(map, VecMap::from([("b", 2), ("a", 1), ("c", 3)]));
source§

impl<K, V> VecMap<K, V>

source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: Eq + ?Sized,

Return true if an equivalent to key exists in the map.

Examples
use vecmap::VecMap;

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

pub fn first(&self) -> Option<(&K, &V)>

Get the first key-value pair.

use vecmap::VecMap;

let mut map = VecMap::from_iter([("a", 1), ("b", 2)]);
assert_eq!(map.first(), Some((&"a", &1)));
source

pub fn first_mut(&mut self) -> Option<(&K, &mut V)>

Get the first key-value pair, with mutable access to the value.

Examples
use vecmap::VecMap;

let mut map = VecMap::from_iter([("a", 1), ("b", 2)]);

if let Some((_, v)) = map.first_mut() {
    *v = *v + 10;
}
assert_eq!(map.first(), Some((&"a", &11)));
source

pub fn last(&self) -> Option<(&K, &V)>

Get the last key-value pair.

Examples
use vecmap::VecMap;

let mut map = VecMap::from_iter([("a", 1), ("b", 2)]);
assert_eq!(map.last(), Some((&"b", &2)));
map.pop();
map.pop();
assert_eq!(map.last(), None);
source

pub fn last_mut(&mut self) -> Option<(&K, &mut V)>

Get the last key-value pair, with mutable access to the value.

Examples
use vecmap::VecMap;

let mut map = VecMap::from_iter([("a", 1), ("b", 2)]);

if let Some((_, v)) = map.last_mut() {
    *v = *v + 10;
}
assert_eq!(map.last(), Some((&"b", &12)));
source

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

Return a reference to the value stored for key, if it is present, else None.

Examples
use vecmap::VecMap;

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

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

Return a mutable reference to the value stored for key, if it is present, else None.

Examples
use vecmap::VecMap;

let mut map = VecMap::new();
map.insert(1, "a");
if let Some(x) = map.get_mut(&1) {
    *x = "b";
}
assert_eq!(map[&1], "b");
source

pub fn get_index(&self, index: usize) -> Option<(&K, &V)>

Return references to the key-value pair stored at index, if it is present, else None.

Examples
use vecmap::VecMap;

let mut map = VecMap::new();
map.insert(1, "a");
assert_eq!(map.get_index(0), Some((&1, &"a")));
assert_eq!(map.get_index(1), None);
source

pub fn get_index_mut(&mut self, index: usize) -> Option<(&K, &mut V)>

Return a reference to the key and a mutable reference to the value stored at index, if it is present, else None.

Examples
use vecmap::VecMap;

let mut map = VecMap::new();
map.insert(1, "a");
if let Some((_, v)) = map.get_index_mut(0) {
    *v = "b";
}
assert_eq!(map[0], "b");
source

pub fn get_full<Q>(&self, key: &Q) -> Option<(usize, &K, &V)>
where K: Borrow<Q>, Q: Eq + ?Sized,

Return the index and references to the key-value pair stored for key, if it is present, else None.

Examples
use vecmap::VecMap;

let mut map = VecMap::new();
map.insert(1, "a");
assert_eq!(map.get_full(&1), Some((0, &1, &"a")));
assert_eq!(map.get_full(&2), None);
source

pub fn get_full_mut<Q>(&mut self, key: &Q) -> Option<(usize, &K, &mut V)>
where K: Borrow<Q>, Q: Eq + ?Sized,

Return the index, a reference to the key and a mutable reference to the value stored for key, if it is present, else None.

Examples
use vecmap::VecMap;

let mut map = VecMap::new();
map.insert(1, "a");

if let Some((_, _, v)) = map.get_full_mut(&1) {
    *v = "b";
}
assert_eq!(map.get(&1), Some(&"b"));
source

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

Return references to the key-value pair stored for key, if it is present, else None.

Examples
use vecmap::VecMap;

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

pub fn get_index_of<Q>(&self, key: &Q) -> Option<usize>
where K: Borrow<Q>, Q: Eq + ?Sized,

Return item index, if it exists in the map.

Examples
use vecmap::VecMap;

let mut map = VecMap::new();
map.insert("a", 10);
map.insert("b", 20);
assert_eq!(map.get_index_of("a"), Some(0));
assert_eq!(map.get_index_of("b"), Some(1));
assert_eq!(map.get_index_of("c"), None);
source§

impl<K, V> VecMap<K, V>

source

pub fn pop(&mut self) -> Option<(K, V)>

Removes the last element from the map and returns it, or None if it is empty.

Examples
use vecmap::VecMap;

let mut map = VecMap::from_iter([("a", 1), ("b", 2)]);
assert_eq!(map.pop(), Some(("b", 2)));
assert_eq!(map.pop(), Some(("a", 1)));
assert!(map.is_empty());
assert_eq!(map.pop(), None);
source

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

Remove the key-value pair equivalent to key and return its value.

Like Vec::remove, the pair is removed by shifting all of the elements that follow it, preserving their relative order. This perturbs the index of all of those elements!

Examples
use vecmap::VecMap;

let mut map = VecMap::from_iter([(1, "a"), (2, "b"), (3, "c"), (4, "d")]);
assert_eq!(map.remove(&2), Some("b"));
assert_eq!(map.remove(&2), None);
assert_eq!(map, VecMap::from_iter([(1, "a"), (3, "c"), (4, "d")]));
source

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

Remove and return the key-value pair equivalent to key.

Like Vec::remove, the pair is removed by shifting all of the elements that follow it, preserving their relative order. This perturbs the index of all of those elements!

Examples
use vecmap::VecMap;

let mut map = VecMap::from_iter([(1, "a"), (2, "b"), (3, "c"), (4, "d")]);
assert_eq!(map.remove_entry(&2), Some((2, "b")));
assert_eq!(map.remove_entry(&2), None);
assert_eq!(map, VecMap::from_iter([(1, "a"), (3, "c"), (4, "d")]));
source

pub fn remove_index(&mut self, index: usize) -> (K, V)

Removes and returns the key-value pair at position index within the map, shifting all elements after it to the left.

If you don’t need the order of elements to be preserved, use swap_remove instead.

Panics

Panics if index is out of bounds.

Examples
use vecmap::VecMap;

let mut v = VecMap::from([("a", 1), ("b", 2), ("c", 3)]);
assert_eq!(v.remove_index(1), ("b", 2));
assert_eq!(v, VecMap::from([("a", 1), ("c", 3)]));
source

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

Remove the key-value pair equivalent to key and return its value.

Like Vec::swap_remove, the pair is removed by swapping it with the last element of the map and popping it off. This perturbs the position of what used to be the last element!

Return None if key is not in map.

use vecmap::VecMap;

let mut map = VecMap::from_iter([(1, "a"), (2, "b"), (3, "c"), (4, "d")]);
assert_eq!(map.swap_remove(&2), Some("b"));
assert_eq!(map.swap_remove(&2), None);
assert_eq!(map, VecMap::from_iter([(1, "a"), (4, "d"), (3, "c")]));
source

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

Remove and return the key-value pair equivalent to key.

Like Vec::swap_remove, the pair is removed by swapping it with the last element of the map and popping it off. This perturbs the position of what used to be the last element!

Return None if key is not in map.

use vecmap::VecMap;

let mut map = VecMap::from_iter([(1, "a"), (2, "b"), (3, "c"), (4, "d")]);
assert_eq!(map.swap_remove_entry(&2), Some((2, "b")));
assert_eq!(map.swap_remove_entry(&2), None);
assert_eq!(map, VecMap::from_iter([(1, "a"), (4, "d"), (3, "c")]));
source

pub fn swap_remove_index(&mut self, index: usize) -> (K, V)

Removes a key-value pair from the map and returns it.

The removed key-value pair is replaced by the last key-value pair of the map.

If you need to preserve the element order, use remove instead.

Panics

Panics if index is out of bounds.

Examples
use vecmap::VecMap;

let mut v = VecMap::from([("foo", 1), ("bar", 2), ("baz", 3), ("qux", 4)]);

assert_eq!(v.swap_remove_index(0), ("foo", 1));
assert_eq!(v, VecMap::from([("qux", 4), ("bar", 2), ("baz", 3)]));

assert_eq!(v.swap_remove_index(0), ("qux", 4));
assert_eq!(v, VecMap::from([("baz", 3), ("bar", 2)]));
source

pub fn swap_indices(&mut self, a: usize, b: usize)

Swaps the position of two key-value pairs in the map.

Arguments
  • a - The index of the first element
  • b - The index of the second element
Panics

Panics if a or b are out of bounds.

Examples
use vecmap::VecMap;

let mut map = VecMap::from([("a", 1), ("b", 2), ("c", 3), ("d", 4)]);
map.swap_indices(1, 3);
assert_eq!(map.to_vec(), [("a", 1), ("d", 4), ("c", 3), ("b", 2)]);
source§

impl<K, V> VecMap<K, V>
where K: Eq,

source

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

Insert a key-value pair in the map.

If an equivalent key already exists in the map: the key remains and retains in its place in the order, its corresponding value is updated with value and the older value is returned inside Some(_).

If no equivalent key existed in the map: the new key-value pair is inserted, last in order, and None is returned.

See also entry if you you want to insert or modify or if you need to get the index of the corresponding key-value pair.

Examples
use vecmap::VecMap;

let mut map = VecMap::new();
assert_eq!(map.insert(37, "a"), None);
assert_eq!(map.is_empty(), false);

map.insert(37, "b");
assert_eq!(map.insert(37, "c"), Some("b"));
assert_eq!(map[&37], "c");
source

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

Insert a key-value pair in the map, and get their index.

If an equivalent key already exists in the map: the key remains and retains in its place in the order, its corresponding value is updated with value and the older value is returned inside (index, Some(_)).

If no equivalent key existed in the map: the new key-value pair is inserted, last in order, and (index, None) is returned.

See also entry if you you want to insert or modify or if you need to get the index of the corresponding key-value pair.

Examples
use vecmap::VecMap;

let mut map = VecMap::new();
assert_eq!(map.insert_full("a", 1), (0, None));
assert_eq!(map.insert_full("b", 2), (1, None));
assert_eq!(map.insert_full("b", 3), (1, Some(2)));
assert_eq!(map["b"], 3);
source

pub fn insert_at( &mut self, index: usize, key: K, value: V ) -> Option<(usize, V)>

Insert a key-value pair at position index within the map, shifting all elements after it to the right.

If an equivalent key already exists in the map: the key is removed from the map and the new key-value pair is inserted at index. The old index and its value are returned inside Some((usize, _)).

If no equivalent key existed in the map: the new key-value pair is inserted at position index and None is returned.

Panics

Panics if index > len.

Examples
use vecmap::VecMap;

let mut map = VecMap::new();
assert_eq!(map.insert_at(0, "a", 1), None);
assert_eq!(map.insert_at(1, "b", 2), None);
assert_eq!(map.insert_at(0, "b", 3), Some((1, 2)));
assert_eq!(map.to_vec(), [("b", 3), ("a", 1)]);
source

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

Get the given key’s corresponding entry in the map for insertion and/or in-place manipulation.

Examples
use vecmap::VecMap;

let mut letters = VecMap::new();

for ch in "a short treatise on fungi".chars() {
    letters.entry(ch).and_modify(|counter| *counter += 1).or_insert(1);
}

assert_eq!(letters[&'s'], 2);
assert_eq!(letters[&'t'], 3);
assert_eq!(letters[&'u'], 1);
assert_eq!(letters.get(&'y'), None);
source§

impl<K, V> VecMap<K, V>

source

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

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

Examples
use vecmap::VecMap;

let map = VecMap::from([
    ("a", 1),
    ("b", 2),
    ("c", 3),
]);

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

pub fn iter_mut(&mut self) -> IterMut<'_, K, V>

An iterator visiting all key-value pairs in insertion order, with mutable references to the values. The iterator element type is (&'a K, &'a mut V).

Examples
use vecmap::VecMap;

let mut map = VecMap::from([
    ("a", 1),
    ("b", 2),
    ("c", 3),
]);

// Update all values
for (_, val) in map.iter_mut() {
    *val *= 2;
}

for (key, val) in &map {
    println!("key: {key} val: {val}");
}
source

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

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

Examples
use vecmap::VecMap;

let map = VecMap::from([
    ("a", 1),
    ("b", 2),
    ("c", 3),
]);

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

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

Creates a consuming iterator visiting all the keys in insertion order. The object cannot be used after calling this. The iterator element type is K.

Examples
use vecmap::VecMap;

let map = VecMap::from([
    ("a", 1),
    ("b", 2),
    ("c", 3),
]);

let mut vec: Vec<&str> = map.into_keys().collect();
assert_eq!(vec, ["a", "b", "c"]);
source

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

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

Examples
use vecmap::VecMap;

let map = VecMap::from([
    ("a", 1),
    ("b", 2),
    ("c", 3),
]);

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

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

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

Examples
use vecmap::VecMap;

let mut map = VecMap::from([
    ("a", 1),
    ("b", 2),
    ("c", 3),
]);

for val in map.values_mut() {
    *val = *val + 10;
}

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

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

Creates a consuming iterator visiting all the values in insertion order. The object cannot be used after calling this. The iterator element type is V.

Examples
use vecmap::VecMap;

let map = VecMap::from([
    ("a", 1),
    ("b", 2),
    ("c", 3),
]);

let mut vec: Vec<i32> = map.into_values().collect();
assert_eq!(vec, [1, 2, 3]);

Trait Implementations§

source§

impl<K: Clone, V: Clone> Clone for VecMap<K, V>

source§

fn clone(&self) -> VecMap<K, V>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<K: Debug, V: Debug> Debug for VecMap<K, V>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<K, V> Default for VecMap<K, V>

source§

fn default() -> Self

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

impl<'de, K, V> Deserialize<'de> for VecMap<K, V>
where K: Deserialize<'de> + Eq, V: Deserialize<'de>,

Available on crate feature serde only.
source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<'a, K: Clone + Eq, V: Clone> Extend<&'a (K, V)> for VecMap<K, V>

source§

fn extend<I>(&mut self, iterable: I)
where I: IntoIterator<Item = &'a (K, V)>,

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<'a, K: Clone + Eq, V: Clone> Extend<(&'a K, &'a V)> for VecMap<K, V>

source§

fn extend<I>(&mut self, iterable: I)
where I: IntoIterator<Item = (&'a K, &'a V)>,

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<K, V> Extend<(K, V)> for VecMap<K, V>
where K: Eq,

source§

fn extend<I>(&mut self, iterable: I)
where I: IntoIterator<Item = (K, V)>,

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<K, V> From<&[(K, V)]> for VecMap<K, V>
where K: Clone + Eq, V: Clone,

source§

fn from(slice: &[(K, V)]) -> Self

Converts to this type from the input type.
source§

impl<K, V> From<&mut [(K, V)]> for VecMap<K, V>
where K: Clone + Eq, V: Clone,

source§

fn from(slice: &mut [(K, V)]) -> Self

Converts to this type from the input type.
source§

impl<K, V, const N: usize> From<[(K, V); N]> for VecMap<K, V>
where K: Eq,

source§

fn from(arr: [(K, V); N]) -> Self

Converts to this type from the input type.
source§

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

source§

fn from(vec: Vec<(K, V)>) -> Self

Constructs map from a vector of (key → value) pairs.

Note: This conversion has a quadratic complexity because the conversion preserves order of elements while at the same time having to make sure no duplicate keys exist. To avoid it, sort and deduplicate the vector and use VecMap::from_vec_unchecked instead.

source§

impl<Item, K, V> FromIterator<Item> for VecMap<K, V>
where Self: Extend<Item>,

source§

fn from_iter<I: IntoIterator<Item = Item>>(iter: I) -> Self

Creates a value from an iterator. Read more
source§

impl<K, V, Q> Index<&Q> for VecMap<K, V>
where K: Borrow<Q>, Q: Eq + ?Sized,

§

type Output = V

The returned type after indexing.
source§

fn index(&self, key: &Q) -> &V

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

impl<K, V> Index<usize> for VecMap<K, V>

§

type Output = V

The returned type after indexing.
source§

fn index(&self, index: usize) -> &V

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

impl<K, V, Q> IndexMut<&Q> for VecMap<K, V>
where K: Borrow<Q>, Q: Eq + ?Sized,

source§

fn index_mut(&mut self, key: &Q) -> &mut V

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

impl<K, V> IndexMut<usize> for VecMap<K, V>

source§

fn index_mut(&mut self, index: usize) -> &mut V

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

impl<'de, K, V, E> IntoDeserializer<'de, E> for VecMap<K, V>
where K: IntoDeserializer<'de, E> + Eq, V: IntoDeserializer<'de, E>, E: Error,

Available on crate feature serde only.
§

type Deserializer = MapDeserializer<'de, <VecMap<K, V> as IntoIterator>::IntoIter, E>

The type of the deserializer being converted into.
source§

fn into_deserializer(self) -> Self::Deserializer

Convert this value into a deserializer.
source§

impl<'a, K, V> IntoIterator for &'a VecMap<K, V>

§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, K, V> IntoIterator for &'a mut VecMap<K, V>

§

type Item = (&'a K, &'a mut V)

The type of the elements being iterated over.
§

type IntoIter = IterMut<'a, K, V>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<K, V> IntoIterator for VecMap<K, V>

§

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?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<K, V> MutableKeys for VecMap<K, V>

§

type Key = K

The map’s key type.
§

type Value = V

The map’s value type.
source§

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

Return the index, a mutable reference to the key and a mutable reference to the value stored for key, if it is present, else None. Read more
source§

fn get_index_mut2(&mut self, index: usize) -> Option<(&mut K, &mut V)>

Return a mutable reference to the key and a mutable reference to the value stored at index, if it is present, else None. Read more
source§

fn retain2<F>(&mut self, f: F)
where F: FnMut(&mut K, &mut V) -> bool,

Retains only the elements specified by the predicate. Read more
source§

fn iter_mut2(&mut self) -> IterMut2<'_, K, V>

An iterator visiting all key-value pairs in insertion order, with mutable references to the values. The iterator element type is (&'a mut K, &'a mut V). Read more
source§

fn keys_mut(&mut self) -> KeysMut<'_, K, V>

An iterator visiting all keys mutably in insertion order. The iterator element type is &'a mut K. Read more
source§

impl<K, V> PartialEq for VecMap<K, V>
where K: Eq, V: PartialEq,

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

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

impl<K, V> Serialize for VecMap<K, V>
where K: Serialize + Eq, V: Serialize,

Available on crate feature serde only.
source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<K, V> Eq for VecMap<K, V>
where K: Eq, V: Eq,

Auto Trait Implementations§

§

impl<K, V> RefUnwindSafe for VecMap<K, V>

§

impl<K, V> Send for VecMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for VecMap<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for VecMap<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for VecMap<K, V>
where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,