[−][src]Struct vec_map::VecMap
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());
Methods
impl<V> VecMap<V>
[src]
impl<V> VecMap<V>
pub fn new() -> Self
[src]
pub fn new() -> Self
pub fn with_capacity(capacity: usize) -> Self
[src]
pub fn with_capacity(capacity: usize) -> Self
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);
pub fn capacity(&self) -> usize
[src]
pub fn capacity(&self) -> usize
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);
pub fn reserve_len(&mut self, len: usize)
[src]
pub fn reserve_len(&mut self, len: usize)
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);
pub fn reserve_len_exact(&mut self, len: usize)
[src]
pub fn reserve_len_exact(&mut self, len: usize)
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);
pub fn shrink_to_fit(&mut self)
[src]
pub fn shrink_to_fit(&mut self)
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);
ⓘImportant traits for Keys<'a, V>pub fn keys(&self) -> Keys<V>
[src]
pub fn keys(&self) -> Keys<V>
Returns an iterator visiting all keys in ascending order of the keys.
The iterator's element type is usize
.
ⓘImportant traits for Values<'a, V>pub fn values(&self) -> Values<V>
[src]
pub fn values(&self) -> Values<V>
Returns an iterator visiting all values in ascending order of the keys.
The iterator's element type is &'r V
.
ⓘImportant traits for ValuesMut<'a, V>pub fn values_mut(&mut self) -> ValuesMut<V>
[src]
pub fn values_mut(&mut self) -> ValuesMut<V>
Returns an iterator visiting all values in ascending order of the keys.
The iterator's element type is &'r mut V
.
ⓘImportant traits for Iter<'a, V>pub fn iter(&self) -> Iter<V>
[src]
pub fn iter(&self) -> Iter<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); }
ⓘImportant traits for IterMut<'a, V>pub fn iter_mut(&mut self) -> IterMut<V>
[src]
pub fn iter_mut(&mut self) -> IterMut<V>
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"); }
pub fn append(&mut self, other: &mut Self)
[src]
pub fn append(&mut self, other: &mut Self)
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");
pub fn split_off(&mut self, at: usize) -> Self
[src]
pub fn split_off(&mut self, at: usize) -> Self
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");
ⓘImportant traits for Drain<'a, V>pub fn drain(&mut self) -> Drain<V>
[src]
pub fn drain(&mut self) -> Drain<V>
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")]);
pub fn len(&self) -> usize
[src]
pub fn len(&self) -> usize
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);
pub fn is_empty(&self) -> bool
[src]
pub fn is_empty(&self) -> bool
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());
pub fn clear(&mut self)
[src]
pub fn clear(&mut self)
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());
pub fn get(&self, key: usize) -> Option<&V>
[src]
pub fn get(&self, key: usize) -> Option<&V>
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);
pub fn contains_key(&self, key: usize) -> bool
[src]
pub fn contains_key(&self, key: usize) -> bool
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);
pub fn get_mut(&mut self, key: usize) -> Option<&mut V>
[src]
pub fn get_mut(&mut self, key: usize) -> Option<&mut V>
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");
pub fn insert(&mut self, key: usize, value: V) -> Option<V>
[src]
pub fn insert(&mut self, key: usize, value: V) -> Option<V>
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");
pub fn remove(&mut self, key: usize) -> Option<V>
[src]
pub fn remove(&mut self, key: usize) -> Option<V>
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);
pub fn entry(&mut self, key: usize) -> Entry<V>
[src]
pub fn entry(&mut self, key: usize) -> Entry<V>
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);
pub fn retain<F>(&mut self, f: F) where
F: FnMut(usize, &mut V) -> bool,
[src]
pub fn retain<F>(&mut self, f: F) where
F: FnMut(usize, &mut V) -> bool,
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
impl<V: PartialEq> PartialEq<VecMap<V>> for VecMap<V>
[src]
impl<V: PartialEq> PartialEq<VecMap<V>> for VecMap<V>
fn eq(&self, other: &Self) -> bool
[src]
fn eq(&self, other: &Self) -> bool
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
This method tests for !=
.
impl<V> Default for VecMap<V>
[src]
impl<V> Default for VecMap<V>
impl<V: Clone> Clone for VecMap<V>
[src]
impl<V: Clone> Clone for VecMap<V>
fn clone(&self) -> Self
[src]
fn clone(&self) -> Self
fn clone_from(&mut self, source: &Self)
[src]
fn clone_from(&mut self, source: &Self)
impl<V: Ord> Ord for VecMap<V>
[src]
impl<V: Ord> Ord for VecMap<V>
fn cmp(&self, other: &Self) -> Ordering
[src]
fn cmp(&self, other: &Self) -> Ordering
fn max(self, other: Self) -> Self
1.21.0[src]
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.21.0[src]
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
impl<T> IntoIterator for VecMap<T>
[src]
impl<T> IntoIterator for VecMap<T>
type Item = (usize, T)
The type of the elements being iterated over.
type IntoIter = IntoIter<T>
Which kind of iterator are we turning this into?
ⓘImportant traits for IntoIter<V>fn into_iter(self) -> IntoIter<T>
[src]
fn into_iter(self) -> IntoIter<T>
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")]);
impl<'a, T> IntoIterator for &'a VecMap<T>
[src]
impl<'a, T> IntoIterator for &'a VecMap<T>
type Item = (usize, &'a T)
The type of the elements being iterated over.
type IntoIter = Iter<'a, T>
Which kind of iterator are we turning this into?
ⓘImportant traits for Iter<'a, V>fn into_iter(self) -> Iter<'a, T>
[src]
fn into_iter(self) -> Iter<'a, T>
impl<'a, T> IntoIterator for &'a mut VecMap<T>
[src]
impl<'a, T> IntoIterator for &'a mut VecMap<T>
type Item = (usize, &'a mut T)
The type of the elements being iterated over.
type IntoIter = IterMut<'a, T>
Which kind of iterator are we turning this into?
ⓘImportant traits for IterMut<'a, V>fn into_iter(self) -> IterMut<'a, T>
[src]
fn into_iter(self) -> IterMut<'a, T>
impl<V> Extend<(usize, V)> for VecMap<V>
[src]
impl<V> Extend<(usize, V)> for VecMap<V>
impl<'a, V: Copy> Extend<(usize, &'a V)> for VecMap<V>
[src]
impl<'a, V: Copy> Extend<(usize, &'a V)> for VecMap<V>
impl<V: Eq> Eq for VecMap<V>
[src]
impl<V: Eq> Eq for VecMap<V>
impl<V: PartialOrd> PartialOrd<VecMap<V>> for VecMap<V>
[src]
impl<V: PartialOrd> PartialOrd<VecMap<V>> for VecMap<V>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn gt(&self, other: &Rhs) -> bool
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn ge(&self, other: &Rhs) -> bool
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<V: Hash> Hash for VecMap<V>
[src]
impl<V: Hash> Hash for VecMap<V>
fn hash<H: Hasher>(&self, state: &mut H)
[src]
fn hash<H: Hasher>(&self, state: &mut H)
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<V> Index<usize> for VecMap<V>
[src]
impl<V> Index<usize> for VecMap<V>
impl<'a, V> Index<&'a usize> for VecMap<V>
[src]
impl<'a, V> Index<&'a usize> for VecMap<V>
impl<V> IndexMut<usize> for VecMap<V>
[src]
impl<V> IndexMut<usize> for VecMap<V>
impl<'a, V> IndexMut<&'a usize> for VecMap<V>
[src]
impl<'a, V> IndexMut<&'a usize> for VecMap<V>
impl<V> FromIterator<(usize, V)> for VecMap<V>
[src]
impl<V> FromIterator<(usize, V)> for VecMap<V>
impl<V: Debug> Debug for VecMap<V>
[src]
impl<V: Debug> Debug for VecMap<V>
Auto Trait Implementations
Blanket Implementations
impl<T> From for T
[src]
impl<T> From for T
impl<I> IntoIterator for I where
I: Iterator,
[src]
impl<I> IntoIterator for I where
I: Iterator,
type Item = <I as Iterator>::Item
The type of the elements being iterated over.
type IntoIter = I
Which kind of iterator are we turning this into?
fn into_iter(self) -> I
[src]
fn into_iter(self) -> I
impl<T, U> Into for T where
U: From<T>,
[src]
impl<T, U> Into for T where
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
impl<T, U> TryFrom for T where
T: From<U>,
[src]
impl<T, U> TryFrom for T where
T: From<U>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T> Borrow for T where
T: ?Sized,
[src]
impl<T> Borrow for T where
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
impl<T> Any for T where
T: 'static + ?Sized,
fn get_type_id(&self) -> TypeId
[src]
fn get_type_id(&self) -> TypeId
impl<T> BorrowMut for T where
T: ?Sized,
[src]
impl<T> BorrowMut for T where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
fn borrow_mut(&mut self) -> &mut T
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,