pub struct Map<K = Key, V = Value>{ /* private fields */ }
Expand description
A hash map implementation with consistent ordering.
Implementations§
Source§impl<K, V> Map<K, V>
impl<K, V> Map<K, V>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty Map
.
§Example
use json_api::value::{Key, Map, Value};
let mut map = Map::<Key, Value>::new();
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new empty Map
, with specified capacity.
§Example
let mut map = Map::with_capacity(2);
map.insert("x", 1);
map.insert("y", 2);
// The next insert will likely require reallocation...
map.insert("z", 3);
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of key-value pairs the map can hold without reallocating.
§Example
let map = Map::<Key, Value>::with_capacity(2);
assert!(map.capacity() >= 2);
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.
§Example
let mut map = Map::new();
map.insert("x", 1);
map.clear();
assert!(map.is_empty());
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.
§Example
let mut map = Map::new();
map.insert(1, "a");
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);
Sourcepub fn drain(&mut self, range: RangeFull) -> Drain<'_, K, V> ⓘ
pub fn drain(&mut self, range: RangeFull) -> Drain<'_, K, V> ⓘ
Clears the map, returning all key-value pairs as an iterator. Keeps the allocated memory for reuse.
§Example
let mut map = Map::new();
map.insert("x", 1);
map.insert("y", 2);
for (key, value) in map.drain(..) {
assert!(key == "x" || key == "y");
assert!(value == 1 || value == 2);
}
assert!(map.is_empty());
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.
§Example
let mut map = Map::new();
map.insert("x", 1);
assert_eq!(map.get("x"), Some(&1));
assert_eq!(map.get("y"), None);
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 a value already existed for key, that old value is returned in
Some
; otherwise, None
is returned.
§Example
let mut map = Map::new();
assert_eq!(map.insert("x", 1), None);
assert_eq!(map.insert("x", 2), Some(1));
Sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, K, V> ⓘ
Return an iterator visiting all the key-value pairs of the map in the order in which they were inserted.
§Example
let mut map = Map::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
for (key, value) in map.iter() {
println!("key: {} value: {}", key, value);
}
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
Return an iterator visiting all the key-value pairs of the map in the order in which they were inserted, with mutable references to the values.
§Example
let mut map = Map::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
for (_, value) in map.iter_mut() {
*value += 1;
}
for (key, value) in &map {
println!("key: {} value: {}", key, value);
}
Sourcepub fn keys(&self) -> Keys<'_, K, V> ⓘ
pub fn keys(&self) -> Keys<'_, K, V> ⓘ
Return an iterator visiting all keys in the order in which they were inserted.
§Example
let mut map = Map::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
for key in map.keys() {
println!("{}", key);
}
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Return the number of key-value pairs in the map.
§Example
let mut map = Map::new();
assert_eq!(map.len(), 0);
map.insert("x", 1);
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.
§Example
let mut map = Map::new();
assert!(map.is_empty());
map.insert("x", 1);
assert!(!map.is_empty());
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.
§Example
let mut map = Map::new();
map.insert("x", 1);
assert_eq!(map.remove("x"), Some(1));
assert_eq!(map.remove("x"), None);
Sourcepub fn values(&self) -> Values<'_, K, V> ⓘ
pub fn values(&self) -> Values<'_, K, V> ⓘ
Return an iterator visiting all values in the order in which they were inserted.
§Example
let mut map = Map::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
for value in map.values() {
println!("{}", value);
}
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
Return an iterator visiting all values mutably in the order in which they were inserted.
§Example
let mut map = Map::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
for value in map.values_mut() {
*value += 1;
}
for value in map.values() {
println!("{}", value);
}
Trait Implementations§
Source§impl<'de, K, V> Deserialize<'de> for Map<K, V>
impl<'de, K, V> Deserialize<'de> for Map<K, V>
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<K, V> Extend<(K, V)> for Map<K, V>
impl<K, V> Extend<(K, V)> for Map<K, V>
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = (K, V)>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = (K, V)>,
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<K, V> FromIterator<(K, V)> for Map<K, V>
impl<K, V> FromIterator<(K, V)> for Map<K, V>
Source§impl<'a, K, V> IntoIterator for &'a Map<K, V>
impl<'a, K, V> IntoIterator for &'a Map<K, V>
Source§impl<'a, K, V> IntoIterator for &'a mut Map<K, V>
impl<'a, K, V> IntoIterator for &'a mut Map<K, V>
Source§impl<K, V> IntoIterator for Map<K, V>
impl<K, V> IntoIterator for Map<K, V>
impl<K, V: Eq> Eq for Map<K, V>
impl<K, V> StructuralPartialEq for Map<K, V>
Auto Trait Implementations§
impl<K, V> Freeze for Map<K, V>
impl<K, V> RefUnwindSafe for Map<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for Map<K, V>
impl<K, V> Sync for Map<K, V>
impl<K, V> Unpin for Map<K, V>
impl<K, V> UnwindSafe for Map<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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.