pub struct VecMap<T> { /* private fields */ }
Implementations§
Source§impl<T> VecMap<T>
impl<T> VecMap<T>
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the map can hold without reallocating.
§Examples
let mut map = VecMap::<()>::new();
assert_eq!(map.capacity(), 0);
map.reserve(10);
assert!(map.capacity() >= 10);
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the map, also referred to as its ‘length’.
§Examples
let mut map = VecMap::<()>::new();
assert_eq!(map.len(), 0);
map.insert(1, ());
assert_eq!(map.len(), 1);
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the map, removing all values. Note that this method has no effect on the allocated capacity of the map.
§Examples
let mut map = VecMap::<()>::new();
map.insert(1, ());
map.clear();
assert_eq!(map.len(), 0);
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more elements to be inserted in this map.
The collection may reserve more space to 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 capacity overflows.
§Examples
let mut map = VecMap::<()>::new();
map.reserve(10);
assert!(map.capacity() >= 10);
Sourcepub fn get(&self, i: usize) -> Option<&T>
pub fn get(&self, i: usize) -> Option<&T>
Returns a reference to the value corresponding to the index i
.
§Examples
let mut map = VecMap::<i32>::new();
let idx = 2;
map.insert(idx, 123);
assert_eq!(map.get(idx), Some(&123));
assert!(map.get(3).is_none());
Sourcepub fn get_mut(&mut self, i: usize) -> Option<&mut T>
pub fn get_mut(&mut self, i: usize) -> Option<&mut T>
Returns a mutable reference to the value corresponding to the index i
.
§Examples
let mut map = VecMap::<i32>::new();
let idx = 1;
map.insert(idx, 123);
*map.get_mut(idx).unwrap() += 1;
assert_eq!(map.get(idx), Some(&124));
Sourcepub fn insert(&mut self, i: usize, v: T) -> Option<T>
pub fn insert(&mut self, i: usize, v: T) -> Option<T>
Inserts value
into the map, allocating more capacity if necessary.
The existing key-value in the map is returned.
§Panics
Panics if the capacity overflows.
§Examples
let mut map = VecMap::<i32>::new();
let idx = 1;
assert!(map.insert(idx, 123).is_none());
assert_eq!(map.insert(idx, 456), Some(123));
assert!(map.insert(0, 123).is_none());
assert_eq!(map.get(idx), Some(&456));
Sourcepub fn remove(&mut self, i: usize) -> Option<T>
pub fn remove(&mut self, i: usize) -> Option<T>
Removes and returns the element at index i
from the map if exists.
§Examples
let mut map = VecMap::<i32>::new();
map.insert(1, 123);
assert_eq!(map.remove(1), Some(123));
assert_eq!(map.remove(1), None);
Sourcepub fn retain(&mut self, f: impl FnMut(usize, &mut T) -> bool)
pub fn retain(&mut self, f: impl FnMut(usize, &mut T) -> bool)
Retains only the elements specified by the predicate, passing a mutable reference to it.
In other words, removes all elements such that f(index, &value)
returns false
.
§Examples
let mut map = VecMap::<i32>::new();
map.insert(1, 1);
map.insert(0, 2);
map.retain(|_, val| { if *val == 1 { *val = 3; true } else { false } });
assert_eq!(map.get(1), Some(&3));
assert_eq!(map.len(), 1);
Sourcepub fn iter(
&self,
) -> FilterMap<Enumerate<Iter<'_, Option<T>>>, fn((usize, &Option<T>)) -> Option<(usize, &T)>> ⓘ
pub fn iter( &self, ) -> FilterMap<Enumerate<Iter<'_, Option<T>>>, fn((usize, &Option<T>)) -> Option<(usize, &T)>> ⓘ
Returns an iterator over this map.
§Examples
let mut map = VecMap::<usize>::new();
for i in 0..10 {
map.insert(i * 2, i * 2);
}
let mut count = 0;
for (i, value) in map.iter() {
assert_eq!(i, count * 2);
count += 1;
}
assert_eq!(count, 10);
Sourcepub fn iter_mut(
&mut self,
) -> FilterMap<Enumerate<IterMut<'_, Option<T>>>, fn((usize, &mut Option<T>)) -> Option<(usize, &mut T)>> ⓘ
pub fn iter_mut( &mut self, ) -> FilterMap<Enumerate<IterMut<'_, Option<T>>>, fn((usize, &mut Option<T>)) -> Option<(usize, &mut T)>> ⓘ
Returns an iterator that allows modifying each value over this map.
§Examples
let mut map = VecMap::<usize>::new();
for i in 0..10 {
map.insert(i * 2, i * 2);
}
let mut count = 0;
for (i, value) in map.iter_mut() {
*value += 1;
assert_eq!(i, count * 2);
assert_eq!(*value, count * 2 + 1);
count += 1;
}
assert_eq!(count, 10);
Trait Implementations§
Source§impl<'de, T: Deserialize<'de>> Deserialize<'de> for VecMap<T>
impl<'de, T: Deserialize<'de>> Deserialize<'de> for VecMap<T>
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<'a, T: Clone + 'a, I: Borrow<usize> + 'a> Extend<(I, &'a T)> for VecMap<T>
impl<'a, T: Clone + 'a, I: Borrow<usize> + 'a> Extend<(I, &'a T)> for VecMap<T>
Source§fn extend<It: IntoIterator<Item = (I, &'a T)>>(&mut self, iter: It)
fn extend<It: IntoIterator<Item = (I, &'a T)>>(&mut self, iter: It)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<T> Extend<(usize, T)> for VecMap<T>
impl<T> Extend<(usize, T)> for VecMap<T>
Source§fn extend<It: IntoIterator<Item = (usize, T)>>(&mut self, iter: It)
fn extend<It: IntoIterator<Item = (usize, T)>>(&mut self, iter: It)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)