pub struct SeqMap<K, V> { /* private fields */ }Expand description
Implementations§
Source§impl<K, V> SeqMap<K, V>
impl<K, V> SeqMap<K, V>
Sourcepub fn new() -> SeqMap<K, V>
pub fn new() -> SeqMap<K, V>
Creates a new, empty SeqMap.
§Examples
use seq_map::SeqMap;
let map: SeqMap<String, i32> = SeqMap::new();Sourcepub fn insert(&mut self, key: K, value: V) -> Result<(), SeqMapError>
pub fn insert(&mut self, key: K, value: V) -> Result<(), SeqMapError>
Inserts a key-value pair into the map.
Returns an error if the key already exists.
§Errors
Returns SeqMapError::KeyAlreadyExists if the key is already present.
§Examples
use seq_map::SeqMap;
let mut map = SeqMap::new();
map.insert("key".to_string(), 42).unwrap();
assert!(map.insert("key".to_string(), 43).is_err());Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Checks if the map contains a key.
Sourcepub fn get_mut(&mut self, key: &K) -> Option<&mut V>
pub fn get_mut(&mut self, key: &K) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the key.
Returns None if the key does not exist.
§Examples
use seq_map::SeqMap;
let mut map = SeqMap::new();
map.insert("key".to_string(), 42).unwrap();
if let Some(value) = map.get_mut(&"key".to_string()) {
*value = 100;
}
assert_eq!(map[&"key".to_string()], 100);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of key-value pairs in the map.
§Examples
use seq_map::SeqMap;
let mut map = SeqMap::new();
assert_eq!(map.len(), 0);
map.insert("key", 42).unwrap();
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 seq_map::SeqMap;
let map: SeqMap<String, i32> = SeqMap::new();
assert!(map.is_empty());Sourcepub fn keys(&self) -> impl Iterator<Item = &K>
pub fn keys(&self) -> impl Iterator<Item = &K>
Returns an iterator over the keys of the map in insertion order.
§Examples
use seq_map::SeqMap;
let mut map = SeqMap::new();
map.insert("a".to_string(), 1).unwrap();
map.insert("b".to_string(), 2).unwrap();
let keys: Vec<_> = map.keys().cloned().collect();
assert_eq!(keys, vec!["a", "b"]);Sourcepub fn values(&self) -> impl Iterator<Item = &V>
pub fn values(&self) -> impl Iterator<Item = &V>
Returns an iterator over the values of the map in insertion order.
§Examples
use seq_map::SeqMap;
let mut map = SeqMap::new();
map.insert("a".to_string(), 1).unwrap();
map.insert("b".to_string(), 2).unwrap();
let values: Vec<_> = map.values().cloned().collect();
assert_eq!(values, vec![1, 2]);pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>
pub fn get_index(&self, key: &K) -> Option<usize>
pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)>
Sourcepub fn get(&self, key: &K) -> Option<&V>
pub fn get(&self, key: &K) -> Option<&V>
Retrieves a reference to the value corresponding to the key.
This method performs a faster lookup using the internal HashMap.
For a slower but simpler lookup, see slow_get.
§Examples
use seq_map::SeqMap;
let mut map = SeqMap::new();
map.insert("key".to_string(), 42).unwrap();
assert_eq!(map.get(&"key".to_string()), Some(&42));Trait Implementations§
Source§impl<K, V> Extend<(K, V)> for SeqMap<K, V>
impl<K, V> Extend<(K, V)> for SeqMap<K, V>
Source§fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = (K, V)>,
fn extend<T>(&mut self, iter: T)where
T: 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> From<&[(K, V)]> for SeqMap<K, V>
impl<K, V> From<&[(K, V)]> for SeqMap<K, V>
Source§fn from(slice: &[(K, V)]) -> SeqMap<K, V>
fn from(slice: &[(K, V)]) -> SeqMap<K, V>
Creates a SeqMap from a slice of key-value pairs.
If duplicate keys are present in the slice, the first occurrence is kept, and subsequent duplicates are ignored.
§Examples
use seq_map::SeqMap;
let pairs = vec![
("a".to_string(), 1),
("b".to_string(), 2),
];
let map: SeqMap<_, _> = SeqMap::from(&pairs[..]);
assert_eq!(map.len(), 2);Source§impl<K, V> FromIterator<(K, V)> for SeqMap<K, V>
impl<K, V> FromIterator<(K, V)> for SeqMap<K, V>
Creates a SeqMap from an iterator of key-value pairs.
If duplicate keys are present in the iterator, the first occurrence is kept, and subsequent duplicates are silently ignored.
Source§impl<K, V> Index<&K> for SeqMap<K, V>
impl<K, V> Index<&K> for SeqMap<K, V>
Source§impl<'a, K, V> IntoIterator for &'a SeqMap<K, V>
impl<'a, K, V> IntoIterator for &'a SeqMap<K, V>
Source§fn into_iter(self) -> <&'a SeqMap<K, V> as IntoIterator>::IntoIter
fn into_iter(self) -> <&'a SeqMap<K, V> as IntoIterator>::IntoIter
Returns an iterator over references to the key-value pairs in insertion order.
This implementation allows you to iterate over references to the keys and values
without consuming the SeqMap. It’s useful for scenarios where you want to inspect
the contents of the map without modifying it.
§Examples
§Iterating Over References
use seq_map::SeqMap;
let mut map = SeqMap::new();
map.insert("a".to_string(), 1).unwrap();
map.insert("b".to_string(), 2).unwrap();
map.insert("c".to_string(), 3).unwrap();
for (key, value) in &map {
println!("{}: {}", key, value);
}§Collecting into a Vector of References
use seq_map::SeqMap;
let mut map = SeqMap::new();
map.insert("x".to_string(), 100).unwrap();
map.insert("y".to_string(), 200).unwrap();
let entries: Vec<_> = (&map).into_iter().collect();
assert_eq!(
entries,
vec![
(&"x".to_string(), &100),
(&"y".to_string(), &200),
]
);