pub struct VecMap<K, V> { /* private fields */ }Expand description
A Vec backed map implementation.
Implementations§
Source§impl<K, V> VecMap<K, V>
impl<K, V> VecMap<K, V>
Sourcepub fn with_capacity(capacity: usize) -> VecMap<K, V>
pub fn with_capacity(capacity: usize) -> VecMap<K, V>
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the map can hold without reallocating.
This number is a lower bound; the VecMap<K, V> might be able to hold
more, but is guaranteed to be able to hold at least this many.
§Examples
use fenn::vec_map::VecMap;
let map: VecMap<i32, i32> = VecMap::with_capacity(100);
assert!(map.capacity() >= 100);Sourcepub fn keys(&self) -> Keys<'_, K, V> ⓘ
pub fn keys(&self) -> Keys<'_, K, V> ⓘ
An iterator visiting all keys in arbitrary order.
The iterator element type is &'a K.
§Examples
use fenn::vec_map::VecMap;
let mut map = VecMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
for key in map.keys() {
println!("{}", key);
}Sourcepub fn values(&self) -> Values<'_, K, V> ⓘ
pub fn values(&self) -> Values<'_, K, V> ⓘ
An iterator visiting all values in arbitrary order.
The iterator element type is &'a V.
§Examples
use fenn::vec_map::VecMap;
let mut map = VecMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
for val in map.values() {
println!("{}", val);
}Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
An iterator visiting all values mutably in arbitrary order.
The iterator element type is &'a mut V.
§Examples
use fenn::vec_map::VecMap;
let mut map = VecMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
for val in map.values_mut() {
*val = *val + 10;
}
for val in map.values() {
println!("{}", val);
}Sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, K, V> ⓘ
An iterator visiting all key-value pairs in arbitrary order.
The iterator element type is (&'a K, &'a V).
§Examples
use fenn::vec_map::VecMap;
let mut map = VecMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
for (key, val) in map.iter() {
println!("key: {} val: {}", key, val);
}Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
An iterator visiting all key-value pairs in arbitrary order, with mutable references to the values.
The iterator element type is (&'a K, &'a mut V).
§Examples
use fenn::vec_map::VecMap;
let mut map = VecMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
// Update all values
for (_, val) in map.iter_mut() {
*val *= 2;
}
for (key, val) in &map {
println!("key: {} val: {}", key, val);
}Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the map.
§Examples
use fenn::vec_map::VecMap;
let mut map = VecMap::new();
assert_eq!(map.len(), 0);
map.insert(1, "a");
assert_eq!(map.len(), 1);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the map contains no elements.
§Examples
use fenn::vec_map::VecMap;
let mut map = VecMap::new();
assert!(map.is_empty());
map.insert(1, "a");
assert!(!map.is_empty());Sourcepub fn drain(&mut self) -> Drain<'_, K, V> ⓘ
pub fn drain(&mut self) -> Drain<'_, K, V> ⓘ
Clears the map, returning all key-value pairs as an iterator. Keeps the allocated memory for reuse.
§Examples
use fenn::vec_map::VecMap;
let mut map = VecMap::new();
map.insert(1, "a");
map.insert(2, "b");
for (k, v) in map.drain().take(1) {
assert!(k == 1 || k == 2);
assert!(v == "a" || v == "b");
}
assert!(map.is_empty());Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
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 fenn::vec_map::VecMap;
let mut map: VecMap<i32, i32> = (0..8).map(|x|(x, x*10)).collect();
map.retain(|&k, _| k % 2 == 0);
assert_eq!(map.len(), 4);Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.
§Examples
use fenn::vec_map::VecMap;
let mut map = VecMap::new();
map.insert(1, "a");
map.clear();
assert!(map.is_empty());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 VecMap. The collection may reserve more space to avoid
frequent reallocations.
§Panics
Panics if the new allocation size overflows usize.
§Examples
use fenn::vec_map::VecMap;
let mut map: VecMap<&str, i32> = VecMap::new();
map.reserve(10);Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the map as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.
§Examples
use fenn::vec_map::VecMap;
let mut map: VecMap<i32, i32> = VecMap::with_capacity(100);
map.insert(1, 2);
map.insert(3, 4);
assert!(map.capacity() >= 100);
map.shrink_to_fit();
assert!(map.capacity() >= 2);pub fn truncate(&mut self, len: usize)
Source§impl<K, V> VecMap<K, V>where
K: Eq,
impl<K, V> VecMap<K, V>where
K: Eq,
Sourcepub fn entry(&mut self, key: K) -> Entry<'_, K, V>
pub fn entry(&mut self, key: K) -> Entry<'_, K, V>
Gets the given key’s corresponding entry in the map for in-place manipulation.
§Examples
use fenn::vec_map::VecMap;
let mut letters = VecMap::new();
for ch in "a short treatise on fungi".chars() {
let counter = letters.entry(ch).or_insert(0);
*counter += 1;
}
assert_eq!(letters[&'s'], 2);
assert_eq!(letters[&'t'], 3);
assert_eq!(letters[&'u'], 1);
assert_eq!(letters.get(&'y'), None);Sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>
pub fn get<Q>(&self, key: &Q) -> Option<&V>
Returns a reference to the value corresponding to the key.
The key may be any borrowed form of the map’s key type, but
PartialEq on the borrowed form must match those for
the key type.
§Examples
use fenn::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);Sourcepub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
Returns the key-value pair corresponding to the supplied key.
The supplied key may be any borrowed form of the map’s key type, but
PartialEq on the borrowed form must match those for
the key type.
§Examples
use fenn::vec_map::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);Sourcepub fn get_key_value_mut<Q>(&mut self, key: &Q) -> Option<(&K, &mut V)>
pub fn get_key_value_mut<Q>(&mut self, key: &Q) -> Option<(&K, &mut V)>
Returns the key-value pair corresponding to the supplied key, with a mutable reference to value.
The supplied key may be any borrowed form of the map’s key type, but
PartialEq on the borrowed form must match those for
the key type.
§Examples
use fenn::vec_map::VecMap;
let mut map = VecMap::new();
map.insert(1, "a");
let (k, v) = map.get_key_value_mut(&1).unwrap();
assert_eq!(k, &1);
assert_eq!(v, &mut "a");
*v = "b";
assert_eq!(map.get_key_value_mut(&1), Some((&1, &mut "b")));
assert_eq!(map.get_key_value_mut(&2), None);Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
Returns true if the map contains a value for the specified key.
The key may be any borrowed form of the map’s key type, but
PartialEq on the borrowed form must match those for
the key type.
§Examples
use fenn::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);Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the key.
The key may be any borrowed form of the map’s key type, but
PartialEq on the borrowed form must match those for
the key type.
§Examples
use fenn::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");Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts a key-value pair into the map.
If the map did not have this key present, None is returned.
If the map did have this key present, the value is updated, and the old
value is returned. The key is not updated, though; this matters for
types that can be == without being identical.
§Examples
use fenn::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");Sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<V>
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
Removes a key from the map, returning the value at the key if the key was previously in the map.
The key may be any borrowed form of the map’s key type, but
PartialEq on the borrowed form must match those for
the key type.
§Examples
use fenn::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);Sourcepub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
Removes a key from the map, returning the stored key and value if the key was previously in the map.
The key may be any borrowed form of the map’s key type, but
PartialEq on the borrowed form must match those for
the key type.
§Examples
use fenn::vec_map::VecMap;
let mut map = VecMap::new();
map.insert(1, "a");
assert_eq!(map.remove_entry(&1), Some((1, "a")));
assert_eq!(map.remove(&1), None);Trait Implementations§
Source§impl<'a, K, V> IntoIterator for &'a VecMap<K, V>
impl<'a, K, V> IntoIterator for &'a VecMap<K, V>
Source§impl<'a, K, V> IntoIterator for &'a mut VecMap<K, V>
impl<'a, K, V> IntoIterator for &'a mut VecMap<K, V>
Source§impl<K, V> IntoIterator for VecMap<K, V>
impl<K, V> IntoIterator for VecMap<K, V>
Source§impl<K, V> VecMapExt<K, V> for VecMap<K, V>
impl<K, V> VecMapExt<K, V> for VecMap<K, V>
Source§fn cleared(self) -> Self
fn cleared(self) -> Self
Source§fn inserted(self, k: K, v: V) -> Selfwhere
K: Eq,
fn inserted(self, k: K, v: V) -> Selfwhere
K: Eq,
Source§fn retained<F>(self, f: F) -> Self
fn retained<F>(self, f: F) -> Self
Source§fn shrinked_to_fit(self) -> Selfwhere
K: Eq,
fn shrinked_to_fit(self) -> Selfwhere
K: Eq,
impl<K, V> Eq for VecMap<K, V>
Auto Trait Implementations§
impl<K, V> Freeze for VecMap<K, V>
impl<K, V> RefUnwindSafe for VecMap<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for VecMap<K, V>
impl<K, V> Sync for VecMap<K, V>
impl<K, V> Unpin for VecMap<K, V>
impl<K, V> UnwindSafe for VecMap<K, V>where
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoIteratorExt for Twhere
T: IntoIterator,
impl<T> IntoIteratorExt for Twhere
T: IntoIterator,
Source§fn chain_if_with<F, C>(self, cond: bool, fun: F) -> ChainIf<T, C> ⓘ
fn chain_if_with<F, C>(self, cond: bool, fun: F) -> ChainIf<T, C> ⓘ
Source§fn chain_if_else_with<FI, I, FE, E>(
self,
cond: bool,
fun_if: FI,
fun_else: FE,
) -> ChainIfElse<T, I, E> ⓘwhere
FI: FnOnce() -> I,
I: IntoIterator<Item = <T as IntoIterator>::Item>,
FE: FnOnce() -> E,
E: IntoIterator<Item = <T as IntoIterator>::Item>,
fn chain_if_else_with<FI, I, FE, E>(
self,
cond: bool,
fun_if: FI,
fun_else: FE,
) -> ChainIfElse<T, I, E> ⓘwhere
FI: FnOnce() -> I,
I: IntoIterator<Item = <T as IntoIterator>::Item>,
FE: FnOnce() -> E,
E: IntoIterator<Item = <T as IntoIterator>::Item>,
Source§fn chain_if<C>(self, cond: bool, iter: C) -> ChainIf<Self, C> ⓘwhere
C: IntoIterator<Item = Self::Item>,
fn chain_if<C>(self, cond: bool, iter: C) -> ChainIf<Self, C> ⓘwhere
C: IntoIterator<Item = Self::Item>,
Source§fn chain_if_else<I, E>(
self,
cond: bool,
iter_if: I,
iter_else: E,
) -> ChainIfElse<Self, I, E> ⓘ
fn chain_if_else<I, E>( self, cond: bool, iter_if: I, iter_else: E, ) -> ChainIfElse<Self, I, E> ⓘ
Source§impl<T> IntoOption for T
impl<T> IntoOption for T
Source§fn some_if(self, predicate: bool) -> Option<Self>
fn some_if(self, predicate: bool) -> Option<Self>
Some(self) if the predicate returns true, or None otherwise.Source§fn with_some_if<F>(self, predicate: F) -> Option<Self>
fn with_some_if<F>(self, predicate: F) -> Option<Self>
Some(self) if the predicate returns true, or None otherwise.Source§impl<T> IntoResult for T
impl<T> IntoResult for T
Source§fn ok_if<E>(self, predicate: bool, err: E) -> Result<Self, E>
fn ok_if<E>(self, predicate: bool, err: E) -> Result<Self, E>
Ok(self) if the predicate returns true, or Err(err) otherwise.Source§fn with_ok_if<F, E>(self, predicate: F, err: E) -> Result<Self, E>
fn with_ok_if<F, E>(self, predicate: F, err: E) -> Result<Self, E>
Ok(self) if the predicate returns true, or Err(err) otherwise.