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

Create a new map. (Does not allocate.)

Examples
use vecmap::VecMap;

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

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

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

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

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

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

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();
assert_eq!(map, VecMap::from_iter([("c", 3), ("b", 2), ("a", 1)]));

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

Computes in O(1) time (amortized average).

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

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

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

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

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

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

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

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Deserialize this value from the given Serde deserializer. Read more

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

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

Extends a collection with exactly one element.

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

Reserves capacity in a collection for the given number of additional elements. Read more

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

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

Extends a collection with exactly one element.

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

Reserves capacity in a collection for the given number of additional elements. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Creates a value from an iterator. Read more

The returned type after indexing.

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

The returned type after indexing.

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

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

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

The type of the deserializer being converted into.

Convert this value into a deserializer.

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 returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. 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

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Serialize this value into the given Serde serializer. 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 resulting type after obtaining ownership.

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

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

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.