pub struct VecMap<V> { /* private fields */ }
Expand description

A map optimized for small integer keys.

Examples

use vec_map::VecMap;

let mut months = VecMap::new();
months.insert(1, "Jan");
months.insert(2, "Feb");
months.insert(3, "Mar");

if !months.contains_key(12) {
    println!("The end is near!");
}

assert_eq!(months.get(1), Some(&"Jan"));

if let Some(value) = months.get_mut(3) {
    *value = "Venus";
}

assert_eq!(months.get(3), Some(&"Venus"));

// Print out all months
for (key, value) in &months {
    println!("month {} is {}", key, value);
}

months.clear();
assert!(months.is_empty());

Implementations

Creates an empty VecMap.

Examples
use vec_map::VecMap;
let mut map: VecMap<&str> = VecMap::new();

Creates an empty VecMap with space for at least capacity elements before resizing.

Examples
use vec_map::VecMap;
let mut map: VecMap<&str> = VecMap::with_capacity(10);

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

Examples
use vec_map::VecMap;
let map: VecMap<String> = VecMap::with_capacity(10);
assert!(map.capacity() >= 10);

Reserves capacity for the given VecMap to contain len distinct keys. In the case of VecMap this means reallocations will not occur as long as all inserted keys are less than len.

The collection may reserve more space to avoid frequent reallocations.

Examples
use vec_map::VecMap;
let mut map: VecMap<&str> = VecMap::new();
map.reserve_len(10);
assert!(map.capacity() >= 10);

Reserves the minimum capacity for the given VecMap to contain len distinct keys. In the case of VecMap this means reallocations will not occur as long as all inserted keys are less than len.

Note that the allocator may give the collection more space than it requests. Therefore capacity cannot be relied upon to be precisely minimal. Prefer reserve_len if future insertions are expected.

Examples
use vec_map::VecMap;
let mut map: VecMap<&str> = VecMap::new();
map.reserve_len_exact(10);
assert!(map.capacity() >= 10);

Trims the VecMap of any excess capacity.

The collection may reserve more space to avoid frequent reallocations.

Examples
use vec_map::VecMap;
let mut map: VecMap<&str> = VecMap::with_capacity(10);
map.shrink_to_fit();
assert_eq!(map.capacity(), 0);

Returns an iterator visiting all keys in ascending order of the keys. The iterator’s element type is usize.

Returns an iterator visiting all values in ascending order of the keys. The iterator’s element type is &'r V.

Returns an iterator visiting all values in ascending order of the keys. The iterator’s element type is &'r mut V.

Returns an iterator visiting all key-value pairs in ascending order of the keys. The iterator’s element type is (usize, &'r V).

Examples
use vec_map::VecMap;

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

// Print `1: a` then `2: b` then `3: c`
for (key, value) in map.iter() {
    println!("{}: {}", key, value);
}

Returns an iterator visiting all key-value pairs in ascending order of the keys, with mutable references to the values. The iterator’s element type is (usize, &'r mut V).

Examples
use vec_map::VecMap;

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

for (key, value) in map.iter_mut() {
    *value = "x";
}

for (key, value) in &map {
    assert_eq!(value, &"x");
}

Moves all elements from other into the map while overwriting existing keys.

Examples
use vec_map::VecMap;

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

let mut b = VecMap::new();
b.insert(3, "c");
b.insert(4, "d");

a.append(&mut b);

assert_eq!(a.len(), 4);
assert_eq!(b.len(), 0);
assert_eq!(a[1], "a");
assert_eq!(a[2], "b");
assert_eq!(a[3], "c");
assert_eq!(a[4], "d");

Splits the collection into two at the given key.

Returns a newly allocated Self. self contains elements [0, at), and the returned Self contains elements [at, max_key).

Note that the capacity of self does not change.

Examples
use vec_map::VecMap;

let mut a = VecMap::new();
a.insert(1, "a");
a.insert(2, "b");
a.insert(3, "c");
a.insert(4, "d");

let b = a.split_off(3);

assert_eq!(a[1], "a");
assert_eq!(a[2], "b");

assert_eq!(b[3], "c");
assert_eq!(b[4], "d");

Returns an iterator visiting all key-value pairs in ascending order of the keys, emptying (but not consuming) the original VecMap. The iterator’s element type is (usize, &'r V). Keeps the allocated memory for reuse.

Examples
use vec_map::VecMap;

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

let vec: Vec<(usize, &str)> = map.drain().collect();

assert_eq!(vec, [(1, "a"), (2, "b"), (3, "c")]);

Returns the number of elements in the map.

Examples
use vec_map::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 elements.

Examples
use vec_map::VecMap;

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

Clears the map, removing all key-value pairs.

Examples
use vec_map::VecMap;

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

Returns a reference to the value corresponding to the key.

Examples
use vec_map::VecMap;

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

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

Examples
use vec_map::VecMap;

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

Returns a mutable reference to the value corresponding to the key.

Examples
use vec_map::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");

Inserts a key-value pair into the map. If the key already had a value present in the map, that value is returned. Otherwise, None is returned.

Examples
use vec_map::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");

Removes a key from the map, returning the value at the key if the key was previously in the map.

Examples
use vec_map::VecMap;

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

Gets the given key’s corresponding entry in the map for in-place manipulation.

Examples
use vec_map::VecMap;

let mut count: VecMap<u32> = VecMap::new();

// count the number of occurrences of numbers in the vec
for x in vec![1, 2, 1, 2, 3, 4, 1, 2, 4] {
    *count.entry(x).or_insert(0) += 1;
}

assert_eq!(count[1], 3);

Retains only the elements specified by the predicate.

In other words, remove all pairs (k, v) such that f(&k, &mut v) returns false.

Examples
use vec_map::VecMap;

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

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

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

Creates a value from an iterator. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. 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

Returns an iterator visiting all key-value pairs in ascending order of the keys, consuming the original VecMap. The iterator’s element type is (usize, &'r V).

Examples
use vec_map::VecMap;

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

let vec: Vec<(usize, &str)> = map.into_iter().collect();

assert_eq!(vec, [(1, "a"), (2, "b"), (3, "c")]);

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

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 !=.

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

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

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

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.