pub struct VecMap<V, I: UnsignedNum = usize> { /* private fields */ }Expand description
Implementations§
Source§impl<V, I: UnsignedNum> VecMap<V, I>
impl<V, I: UnsignedNum> VecMap<V, I>
Sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a new, empty VecMap.
It will not allocate until elements are pushed onto it.
§Examples
let map = VecMap::<()>::new();Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the map can hold without reallocating.
§Examples
let map = VecMap::<()>::with_capacity(10);
assert_eq!(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 get(&self, i: &I) -> Option<&V>
pub fn get(&self, i: &I) -> Option<&V>
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));
map.remove(&idx);
assert!(map.get(&idx).is_none());Sourcepub fn get_mut(&mut self, i: &I) -> Option<&mut V>
pub fn get_mut(&mut self, i: &I) -> Option<&mut V>
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.remove(&idx), Some(124));
assert!(map.get_mut(&idx).is_none());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!(map.len() == 0);Sourcepub fn insert(&mut self, i: I, v: V) -> Option<V>
pub fn insert(&mut self, i: I, v: V) -> Option<V>
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).unwrap(), 123);
assert!(map.insert(0, 123).is_none());
assert_eq!(*map.get(&idx).unwrap(), 456);Sourcepub fn remove(&mut self, i: &I) -> Option<V>
pub fn remove(&mut self, i: &I) -> Option<V>
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(&I, &mut V) -> bool)
pub fn retain(&mut self, f: impl FnMut(&I, &mut V) -> 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).unwrap(), 3);
assert!(map.get(&0).is_none());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 the given 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);Trait Implementations§
Source§impl<V, I: UnsignedNum> Default for VecMap<V, I>
impl<V, I: UnsignedNum> Default for VecMap<V, I>
Source§impl<'de, V, I> Deserialize<'de> for VecMap<V, I>where
V: Deserialize<'de>,
I: UnsignedNum,
impl<'de, V, I> Deserialize<'de> for VecMap<V, I>where
V: Deserialize<'de>,
I: UnsignedNum,
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, V, I> Extend<(&'a I, &'a V)> for VecMap<V, I>where
I: UnsignedNum + 'a,
V: Copy + 'a,
impl<'a, V, I> Extend<(&'a I, &'a V)> for VecMap<V, I>where
I: UnsignedNum + 'a,
V: Copy + 'a,
Source§fn extend<It: IntoIterator<Item = (&'a I, &'a V)>>(&mut self, iter: It)
fn extend<It: IntoIterator<Item = (&'a I, &'a V)>>(&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<'a, V, I> Extend<(I, &'a V)> for VecMap<V, I>where
I: UnsignedNum + 'a,
V: Copy + 'a,
impl<'a, V, I> Extend<(I, &'a V)> for VecMap<V, I>where
I: UnsignedNum + 'a,
V: Copy + 'a,
Source§fn extend<It: IntoIterator<Item = (I, &'a V)>>(&mut self, iter: It)
fn extend<It: IntoIterator<Item = (I, &'a V)>>(&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<V, I: UnsignedNum> Extend<(I, V)> for VecMap<V, I>
impl<V, I: UnsignedNum> Extend<(I, V)> for VecMap<V, I>
Source§fn extend<It: IntoIterator<Item = (I, V)>>(&mut self, iter: It)
fn extend<It: IntoIterator<Item = (I, V)>>(&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<V, I: UnsignedNum> FromIterator<(I, V)> for VecMap<V, I>
impl<V, I: UnsignedNum> FromIterator<(I, V)> for VecMap<V, I>
Source§impl<V, I: UnsignedNum> Index<I> for VecMap<V, I>
impl<V, I: UnsignedNum> Index<I> for VecMap<V, I>
Source§impl<V, I: UnsignedNum> IndexMut<I> for VecMap<V, I>
impl<V, I: UnsignedNum> IndexMut<I> for VecMap<V, I>
Source§impl<'a, V, I: UnsignedNum> IntoIterator for &'a VecMap<V, I>
impl<'a, V, I: UnsignedNum> IntoIterator for &'a VecMap<V, I>
Source§impl<'a, V, I: UnsignedNum> IntoIterator for &'a mut VecMap<V, I>
impl<'a, V, I: UnsignedNum> IntoIterator for &'a mut VecMap<V, I>
Source§impl<V, I: UnsignedNum> IntoIterator for VecMap<V, I>
impl<V, I: UnsignedNum> IntoIterator for VecMap<V, I>
Source§impl<V, I: UnsignedNum> Map for VecMap<V, I>
impl<V, I: UnsignedNum> Map for VecMap<V, I>
Source§fn len(&self) -> usize
fn len(&self) -> usize
Source§fn get(&self, i: &Self::Key) -> Option<&Self::Value>
fn get(&self, i: &Self::Key) -> Option<&Self::Value>
key if exists.Source§fn contains_key(&self, key: &Self::Key) -> bool
fn contains_key(&self, key: &Self::Key) -> bool
true if the map contains a value for the key.Source§impl<V, I: UnsignedNum> MapMut for VecMap<V, I>
impl<V, I: UnsignedNum> MapMut for VecMap<V, I>
Source§fn get_mut(&mut self, i: &Self::Key) -> Option<&mut Self::Value>
fn get_mut(&mut self, i: &Self::Key) -> Option<&mut Self::Value>
key if exists.Source§fn insert(&mut self, i: Self::Key, v: Self::Value) -> Option<Self::Value>
fn insert(&mut self, i: Self::Key, v: Self::Value) -> Option<Self::Value>
value into the map. The existing value in the map is returned.