### Create IndexSet from Array Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Example demonstrating the conversion of an array into an IndexSet. ```rust use indexmap::IndexSet; ``` -------------------------------- ### Create an IndexMap with the indexmap macro Source: https://docs.rs/indexmap/2.14.0/indexmap/macro.indexmap.html Example demonstrating the initialization of an IndexMap and basic lookup operations. ```rust use indexmap::indexmap; let map = indexmap!{ "a" => 1, "b" => 2, }; assert_eq!(map["a"], 1); assert_eq!(map["b"], 2); assert_eq!(map.get("c"), None); // "a" is the first key assert_eq!(map.keys().next(), Some(&"a")); ``` -------------------------------- ### Create IndexMap from Array Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Example of initializing an IndexMap from a fixed-size array. ```rust use indexmap::IndexMap; ``` -------------------------------- ### Creating an IndexMap with a custom hasher Source: https://docs.rs/indexmap/2.14.0/indexmap/macro.indexmap_with_default.html Example usage of the indexmap_with_default macro to initialize a map with FnvHasher. ```rust use indexmap::indexmap_with_default; use fnv::FnvHasher; let map = indexmap_with_default!{ FnvHasher; "a" => 1, "b" => 2, }; assert_eq!(map["a"], 1); assert_eq!(map["b"], 2); assert_eq!(map.get("c"), None); // "a" is the first key assert_eq!(map.keys().next(), Some(&"a")); ``` -------------------------------- ### Panic on out of bounds indexing Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.Keys.html Example showing that indexing out of bounds will cause a panic. ```rust use indexmap::IndexMap; let mut map = IndexMap::new(); map.insert("foo", 1); println!("{:?}", map.keys()[10]); // panics! ``` -------------------------------- ### Shift Insert Invalid Example Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Demonstrates an invalid usage of shift_insert for moving an existing key. ```rust // This is an invalid index for moving an existing key! map.shift_insert(map.len(), 'a', ()); ``` -------------------------------- ### get Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Retrieves a reference to the value stored in the set. ```APIDOC ## pub fn get(&self, value: &Q) -> Option<&T> ### Description Return a reference to the value stored in the set, if it is present, else `None`. ### Complexity Computes in O(1) time (average). ``` -------------------------------- ### IndexSet Creation Methods Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Methods for initializing a new IndexSet instance. ```APIDOC ## pub fn new() -> Self ### Description Create a new empty set. Does not allocate memory initially. ## pub fn with_capacity(n: usize) -> Self ### Description Create a new set with capacity for `n` elements. Does not allocate if `n` is zero. Computes in O(n) time. ## pub fn with_capacity_and_hasher(n: usize, hash_builder: S) -> Self ### Description Create a new set with capacity for `n` elements and a custom hash builder. Computes in O(n) time. ## pub const fn with_hasher(hash_builder: S) -> Self ### Description Create a new set with a custom `hash_builder`. This function is `const` and can be used in `static` contexts. ``` -------------------------------- ### Create an IndexSet using the indexset macro Source: https://docs.rs/indexmap/2.14.0/indexmap/macro.indexset.html Initializes an IndexSet with string literals and verifies membership and insertion order. ```rust use indexmap::indexset; let set = indexset!{ "a", "b", }; assert!(set.contains("a")); assert!(set.contains("b")); assert!(!set.contains("c")); // "a" is the first value assert_eq!(set.iter().next(), Some(&"a")); ``` -------------------------------- ### Create IndexSet with custom hasher Source: https://docs.rs/indexmap/2.14.0/indexmap/macro.indexset_with_default.html Demonstrates initializing an IndexSet with the FnvHasher using the indexset_with_default macro. ```rust use indexmap::indexset_with_default; use fnv::FnvHasher; let set = indexset_with_default!{ FnvHasher; "a", "b", }; assert!(set.contains("a")); assert!(set.contains("b")); assert!(!set.contains("c")); // "a" is the first value assert_eq!(set.iter().next(), Some(&"a")); ``` -------------------------------- ### Get disjoint indices in IndexMap Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Retrieves multiple key-value pairs by unique indices. Requires all provided indices to be within bounds and unique. ```rust let mut map = indexmap::IndexMap::from([(1, 'a'), (3, 'b'), (2, 'c')]); assert_eq!(map.get_disjoint_indices_mut([2, 0]), Ok([(&2, &mut 'c'), (&1, &mut 'a')])); ``` -------------------------------- ### Configuration Methods Source: https://docs.rs/indexmap/2.14.0/indexmap/map/rayon/struct.ParIter.html Methods for configuring parallel execution parameters. ```APIDOC ## fn with_min_len(self, min: usize) -> MinLen Sets the minimum length of iterators desired to process in each rayon job. ## fn with_max_len(self, max: usize) -> MaxLen Sets the maximum length of iterators desired to process in each rayon job. ``` -------------------------------- ### indexmap! Macro Source: https://docs.rs/indexmap/2.14.0/indexmap/macro.indexmap.html The indexmap! macro allows for the initialization of an IndexMap with a specified set of key-value pairs. ```APIDOC ## indexmap! ### Description Creates an `IndexMap` from a list of key-value pairs. This macro is available when the `std` feature is enabled. ### Syntax `indexmap! { key => value, ... }` ### Example ```rust use indexmap::indexmap; let map = indexmap!{ "a" => 1, "b" => 2, }; assert_eq!(map["a"], 1); assert_eq!(map["b"], 2); ``` ``` -------------------------------- ### Using Alternate Hashers with IndexMap Source: https://docs.rs/indexmap/2.14.0/indexmap/index.html Demonstrates how to define type aliases for IndexMap and IndexSet using a custom hasher like FnvBuildHasher. ```rust use fnv::FnvBuildHasher; use indexmap::{IndexMap, IndexSet}; type FnvIndexMap = IndexMap; type FnvIndexSet = IndexSet; let std: IndexSet = (0..100).collect(); let fnv: FnvIndexSet = (0..100).collect(); assert_eq!(std, fnv); ``` -------------------------------- ### Accessing IndexMap with raw_entry_v1 Source: https://docs.rs/indexmap/2.14.0/indexmap/map/raw_entry_v1/trait.RawEntryApiV1.html Demonstrates using the immutable raw entry builder to search for keys using hashes or custom comparison logic. ```rust use core::hash::BuildHasher; use indexmap::map::{IndexMap, RawEntryApiV1}; let mut map = IndexMap::new(); map.extend([("a", 100), ("b", 200), ("c", 300)]); for k in ["a", "b", "c", "d", "e", "f"] { let hash = map.hasher().hash_one(k); let i = map.get_index_of(k); let v = map.get(k); let kv = map.get_key_value(k); let ikv = map.get_full(k); println!("Key: {} and value: {:?}", k, v); assert_eq!(map.raw_entry_v1().from_key(k), kv); assert_eq!(map.raw_entry_v1().from_hash(hash, |q| *q == k), kv); assert_eq!(map.raw_entry_v1().from_key_hashed_nocheck(hash, k), kv); assert_eq!(map.raw_entry_v1().from_hash_full(hash, |q| *q == k), ikv); assert_eq!(map.raw_entry_v1().index_from_hash(hash, |q| *q == k), i); } ``` -------------------------------- ### Create IndexMap from Iterator Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Demonstrates creating an IndexMap using FromIterator. ```rust let map1 = IndexMap::from([(1, 2), (3, 4)]); let map2: IndexMap<_, _> = [(1, 2), (3, 4)].into(); assert_eq!(map1, map2); ``` -------------------------------- ### pub fn entry(&mut self, key: K) -> Entry<'_, K, V> Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Provides access to a key's entry for insertion or in-place manipulation. ```APIDOC ## entry ### Description Get the given key’s corresponding entry in the map for insertion and/or in-place manipulation. ### Parameters - **key** (K) - The key to retrieve the entry for. ### Returns - **Entry<'_, K, V>** - An entry handle for the specified key. ``` -------------------------------- ### indexset_with_default! Source: https://docs.rs/indexmap/2.14.0/indexmap/macro.indexset_with_default.html Creates an IndexSet from a list of values and a BuildHasherDefault-wrapped custom hasher. ```APIDOC ## indexset_with_default! ### Description Creates an `IndexSet` from a list of values and a `BuildHasherDefault`-wrapped custom hasher. ### Signature ```rust macro_rules! indexset_with_default { ($H:ty; $($value:expr,)+) => { ... }; ($H:ty; $($value:expr),*) => { ... }; } ``` ### Example ```rust use indexmap::indexset_with_default; use fnv::FnvHasher; let set = indexset_with_default!{ FnvHasher; "a", "b", }; assert!(set.contains("a")); assert!(set.contains("b")); assert!(!set.contains("c")); // "a" is the first value assert_eq!(set.iter().next(), Some(&"a")); ``` ``` -------------------------------- ### indexset! Source: https://docs.rs/indexmap/2.14.0/indexmap/macro.indexset.html Creates an IndexSet from a list of values. This macro is available when the 'std' feature is enabled. ```APIDOC ## indexset! ### Description Creates an `IndexSet` from a list of values. The macro supports both trailing commas and standard comma-separated lists. ### Signature `macro_rules! indexset { ($($value:expr,)+) => { ... }; ($($value:expr),*) => { ... }; }` ### Requirements - Requires the `std` crate feature. ### Example ```rust use indexmap::indexset; let set = indexset!{ "a", "b", }; assert!(set.contains("a")); assert!(set.contains("b")); ``` ``` -------------------------------- ### take_any Source: https://docs.rs/indexmap/2.14.0/indexmap/map/rayon/struct.ParDrain.html Creates an iterator that yields n elements from anywhere in the original iterator. ```APIDOC ### fn take_any(self, n: usize) -> TakeAny Creates an iterator that yields `n` elements from anywhere in the original iterator. ``` -------------------------------- ### take_any Source: https://docs.rs/indexmap/2.14.0/indexmap/map/rayon/struct.IntoParIter.html Creates an iterator that yields n elements from anywhere in the original iterator. ```APIDOC ## fn take_any(self, n: usize) -> TakeAny ### Description Creates an iterator that yields `n` elements from anywhere in the original iterator. ``` -------------------------------- ### Manipulating IndexMap with raw_entry_mut_v1 Source: https://docs.rs/indexmap/2.14.0/indexmap/map/raw_entry_v1/trait.RawEntryApiV1.html Shows how to use the mutable raw entry builder to perform insertions, updates, and removals on map entries. ```rust use core::hash::BuildHasher; use indexmap::map::{IndexMap, RawEntryApiV1}; use indexmap::map::raw_entry_v1::RawEntryMut; let mut map = IndexMap::new(); map.extend([("a", 100), ("b", 200), ("c", 300)]); // Existing key (insert and update) match map.raw_entry_mut_v1().from_key("a") { RawEntryMut::Vacant(_) => unreachable!(), RawEntryMut::Occupied(mut view) => { assert_eq!(view.index(), 0); assert_eq!(view.get(), &100); let v = view.get_mut(); let new_v = (*v) * 10; *v = new_v; assert_eq!(view.insert(1111), 1000); } } assert_eq!(map["a"], 1111); assert_eq!(map.len(), 3); // Existing key (take) let hash = map.hasher().hash_one("c"); match map.raw_entry_mut_v1().from_key_hashed_nocheck(hash, "c") { RawEntryMut::Vacant(_) => unreachable!(), RawEntryMut::Occupied(view) => { assert_eq!(view.index(), 2); assert_eq!(view.shift_remove_entry(), ("c", 300)); } } assert_eq!(map.raw_entry_v1().from_key("c"), None); assert_eq!(map.len(), 2); // Nonexistent key (insert and update) let key = "d"; let hash = map.hasher().hash_one(key); match map.raw_entry_mut_v1().from_hash(hash, |q| *q == key) { RawEntryMut::Occupied(_) => unreachable!(), RawEntryMut::Vacant(view) => { assert_eq!(view.index(), 2); let (k, value) = view.insert("d", 4000); assert_eq!((*k, *value), ("d", 4000)); *value = 40000; } } assert_eq!(map["d"], 40000); assert_eq!(map.len(), 3); match map.raw_entry_mut_v1().from_hash(hash, |q| *q == key) { RawEntryMut::Vacant(_) => unreachable!(), RawEntryMut::Occupied(view) => { assert_eq!(view.index(), 2); assert_eq!(view.swap_remove_entry(), ("d", 40000)); } } assert_eq!(map.get("d"), None); assert_eq!(map.len(), 2); ``` -------------------------------- ### Index keys in IndexMap Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.Keys.html Demonstrates indexing into the keys of an IndexMap and how iterator advancement affects indexing. ```rust use indexmap::IndexMap; let mut map = IndexMap::new(); for word in "Lorem ipsum dolor sit amet".split_whitespace() { map.insert(word.to_lowercase(), word.to_uppercase()); } assert_eq!(map[0], "LOREM"); assert_eq!(map.keys()[0], "lorem"); assert_eq!(map[1], "IPSUM"); assert_eq!(map.keys()[1], "ipsum"); map.reverse(); assert_eq!(map.keys()[0], "amet"); assert_eq!(map.keys()[1], "sit"); map.sort_keys(); assert_eq!(map.keys()[0], "amet"); assert_eq!(map.keys()[1], "dolor"); // Advancing the iterator will offset the indexing let mut keys = map.keys(); assert_eq!(keys[0], "amet"); assert_eq!(keys.next().map(|s| &**s), Some("amet")); assert_eq!(keys[0], "dolor"); assert_eq!(keys[1], "ipsum"); // Slices may have an offset as well let slice = &map[2..]; assert_eq!(slice[0], "IPSUM"); assert_eq!(slice.keys()[0], "ipsum"); ``` -------------------------------- ### indexmap_with_default! Source: https://docs.rs/indexmap/2.14.0/indexmap/macro.indexmap_with_default.html A macro to create an IndexMap with a custom hasher. It accepts a hasher type followed by a comma-separated list of key-value pairs. ```APIDOC ## indexmap_with_default! ### Description Creates an `IndexMap` from a list of key-value pairs and a `BuildHasherDefault`-wrapped custom hasher. ### Signature `macro_rules! indexmap_with_default { ($H:ty; $($key:expr => $value:expr,)+) => { ... }; ($H:ty; $($key:expr => $value:expr),*) => { ... }; }` ### Example ```rust use indexmap::indexmap_with_default; use fnv::FnvHasher; let map = indexmap_with_default!{ FnvHasher; "a" => 1, "b" => 2, }; assert_eq!(map["a"], 1); assert_eq!(map["b"], 2); ``` ``` -------------------------------- ### Create IndexSet from iterators Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Demonstrates initializing an IndexSet using the FromIterator trait. ```rust let set1 = IndexSet::from([1, 2, 3, 4]); let set2: IndexSet<_> = [1, 2, 3, 4].into(); assert_eq!(set1, set2); ``` -------------------------------- ### Insert entries before a specific index in IndexMap Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Demonstrates using insert_before to place keys at a specific index, shifting existing elements as necessary. ```rust use indexmap::IndexMap; let mut map: IndexMap = ('a'..='z').map(|c| (c, ())).collect(); // The new key '*' goes exactly at the given index. assert_eq!(map.get_index_of(&'*'), None); assert_eq!(map.insert_before(10, '*', ()), (10, None)); assert_eq!(map.get_index_of(&'*'), Some(10)); // Moving the key 'a' up will shift others down, so this moves *before* 10 to index 9. assert_eq!(map.insert_before(10, 'a', ()), (9, Some(()))); assert_eq!(map.get_index_of(&'a'), Some(9)); assert_eq!(map.get_index_of(&'*'), Some(10)); // Moving the key 'z' down will shift others up, so this moves to exactly 10. assert_eq!(map.insert_before(10, 'z', ()), (10, Some(()))); assert_eq!(map.get_index_of(&'z'), Some(10)); assert_eq!(map.get_index_of(&'*'), Some(11)); // Moving or inserting before the endpoint is also valid. assert_eq!(map.len(), 27); assert_eq!(map.insert_before(map.len(), '*', ()), (26, Some(()))); assert_eq!(map.get_index_of(&'*'), Some(26)); assert_eq!(map.insert_before(map.len(), '+', ()), (27, None)); assert_eq!(map.get_index_of(&'+'), Some(27)); assert_eq!(map.len(), 28); ``` -------------------------------- ### Shift and insert entries in IndexMap Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Demonstrates using shift_insert to insert or move keys to a specific index, with different bounds constraints compared to insert_before. ```rust use indexmap::IndexMap; let mut map: IndexMap = ('a'..='z').map(|c| (c, ())).collect(); // The new key '*' goes exactly at the given index. assert_eq!(map.get_index_of(&'*'), None); assert_eq!(map.shift_insert(10, '*', ()), None); assert_eq!(map.get_index_of(&'*'), Some(10)); // Moving the key 'a' up to 10 will shift others down, including the '*' that was at 10. assert_eq!(map.shift_insert(10, 'a', ()), Some(())); assert_eq!(map.get_index_of(&'a'), Some(10)); assert_eq!(map.get_index_of(&'*'), Some(9)); // Moving the key 'z' down to 9 will shift others up, including the '*' that was at 9. assert_eq!(map.shift_insert(9, 'z', ()), Some(())); assert_eq!(map.get_index_of(&'z'), Some(9)); assert_eq!(map.get_index_of(&'*'), Some(10)); // Existing keys can move to len-1 at most, but new keys can insert at the endpoint. assert_eq!(map.len(), 27); assert_eq!(map.shift_insert(map.len() - 1, '*', ()), Some(())); assert_eq!(map.get_index_of(&'*'), Some(26)); assert_eq!(map.shift_insert(map.len(), '+', ()), None); assert_eq!(map.get_index_of(&'+'), Some(27)); assert_eq!(map.len(), 28); ``` ```rust use indexmap::IndexMap; let mut map: IndexMap = ('a'..='z').map(|c| (c, ())).collect(); ``` -------------------------------- ### fn raw_entry_mut_v1(&mut self) -> RawEntryBuilderMut<'_, K, V, S> Source: https://docs.rs/indexmap/2.14.0/indexmap/map/raw_entry_v1/trait.RawEntryApiV1.html Creates a raw mutable entry builder for the IndexMap, allowing for low-level manipulation of map entries. ```APIDOC ## fn raw_entry_mut_v1(&mut self) -> RawEntryBuilderMut<'_, K, V, S> ### Description Creates a raw entry builder for the `IndexMap` that allows for mutable access. This provides low-level control for searching and manipulating the map, including insertions and removals. ### Warning Because raw entries provide low-level control, it is easier to put the `IndexMap` into an inconsistent state. Users must ensure that mutable access to keys does not modify how the key compares or hashes, as this can lead to 'lost' keys or erratic search behavior. ``` -------------------------------- ### Define indexset_with_default macro signature Source: https://docs.rs/indexmap/2.14.0/indexmap/macro.indexset_with_default.html Shows the macro_rules definition for indexset_with_default. ```rust macro_rules! indexset_with_default { ($H:ty; $($value:expr,)+) => { ... }; ($H:ty; $($value:expr),*) => { ... }; } ``` -------------------------------- ### insert_full Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Inserts a key-value pair and returns the index and any previous value. ```APIDOC ## pub fn insert_full(&mut self, key: K, value: V) -> (usize, Option) ### Description Inserts a key-value pair and returns the index of the key and the previous value if it existed. ``` -------------------------------- ### fn raw_entry_v1(&self) -> RawEntryBuilder<'_, K, V, S> Source: https://docs.rs/indexmap/2.14.0/indexmap/map/raw_entry_v1/trait.RawEntryApiV1.html Creates a raw immutable entry builder for the IndexMap. This provides low-level control for searching the map, useful for hash memoization or custom comparison logic. ```APIDOC ## fn raw_entry_v1(&self) -> RawEntryBuilder<'_, K, V, S> ### Description Creates a raw immutable entry builder for the `IndexMap`. Raw entries provide the lowest level of control for searching a map and must be manually initialized with a hash and searched. ### Usage This method is intended for advanced use cases such as hash memoization, using search keys incompatible with the `Equivalent` trait, or implementing custom comparison logic. ``` -------------------------------- ### partition Source: https://docs.rs/indexmap/2.14.0/indexmap/set/rayon/struct.ParIter.html Partitions items into two containers based on a predicate. ```APIDOC ## fn partition(self, predicate: P) -> (A, B) ### Description Partitions the items of a parallel iterator into a pair of arbitrary ParallelExtend containers. Items for which the predicate returns true go into the first container, and the rest go into the second. ``` -------------------------------- ### try_reserve_exact Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Attempts to reserve exact capacity for additional values without over-allocation. ```APIDOC ## pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> ### Description Try to reserve capacity for `additional` more values, without over-allocating. ``` -------------------------------- ### Capacity Management Methods Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Methods for managing the internal capacity of the IndexMap. ```APIDOC ## Capacity Management - **reserve(additional: usize)**: Reserve capacity for additional key-value pairs. - **reserve_exact(additional: usize)**: Reserve capacity without deliberate over-allocation. - **try_reserve(additional: usize) -> Result<(), TryReserveError>**: Attempt to reserve capacity. - **try_reserve_exact(additional: usize) -> Result<(), TryReserveError>**: Attempt to reserve capacity without over-allocation. - **shrink_to_fit()**: Shrink capacity as much as possible. - **shrink_to(min_capacity: usize)**: Shrink capacity to a specified lower limit. ``` -------------------------------- ### Count character frequency with IndexMap Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Demonstrates using IndexMap to count character occurrences while maintaining insertion order. ```rust use indexmap::IndexMap; // count the frequency of each letter in a sentence. let mut letters = IndexMap::new(); for ch in "a short treatise on fungi".chars() { *letters.entry(ch).or_insert(0) += 1; } assert_eq!(letters[&'s'], 2); assert_eq!(letters[&'t'], 3); assert_eq!(letters[&'u'], 1); assert_eq!(letters.get(&'y'), None); ``` -------------------------------- ### Collect characters into an IndexSet Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Demonstrates collecting characters from a string into an IndexSet to track unique elements while maintaining order. ```rust use indexmap::IndexSet; // Collects which letters appear in a sentence. let letters: IndexSet<_> = "a short treatise on fungi".chars().collect(); assert!(letters.contains(&'s')); assert!(letters.contains(&'t')); assert!(letters.contains(&'u')); assert!(!letters.contains(&'y')); ``` -------------------------------- ### Define Keys struct Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.Keys.html The definition of the Keys struct. ```rust pub struct Keys<'a, K, V> { /* private fields */ } ``` -------------------------------- ### get_full Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Retrieves the index and a reference to the value stored in the set. ```APIDOC ## pub fn get_full(&self, value: &Q) -> Option<(usize, &T)> ### Description Return item index and value if present in the set. ``` -------------------------------- ### insert_full Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Inserts a value into the set and returns its index. ```APIDOC ## pub fn insert_full(&mut self, value: T) -> (usize, bool) ### Description Insert the value into the set, and get its index. Returns the index and a boolean indicating if the value was newly inserted. ``` -------------------------------- ### Access IndexMap values by index Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Demonstrates accessing and manipulating values by their numerical index. Note that indexing out of bounds will cause a panic. ```rust use indexmap::IndexMap; let mut map = IndexMap::new(); for word in "Lorem ipsum dolor sit amet".split_whitespace() { map.insert(word.to_lowercase(), word.to_uppercase()); } assert_eq!(map[0], "LOREM"); assert_eq!(map[1], "IPSUM"); map.reverse(); assert_eq!(map[0], "AMET"); assert_eq!(map[1], "SIT"); map.sort_keys(); assert_eq!(map[0], "AMET"); assert_eq!(map[1], "DOLOR"); ``` ```rust use indexmap::IndexMap; let mut map = IndexMap::new(); map.insert("foo", 1); println!("{:?}", map[10]); // panics! ``` -------------------------------- ### from_key Source: https://docs.rs/indexmap/2.14.0/indexmap/map/raw_entry_v1/struct.RawEntryBuilderMut.html Access an entry in the map using a key. ```APIDOC ### pub fn from_key(self, key: &Q) -> RawEntryMut<'a, K, V, S> Access an entry by key. Requires the key to implement Hash and Equivalent. ``` -------------------------------- ### from_hash Source: https://docs.rs/indexmap/2.14.0/indexmap/map/raw_entry_v1/struct.RawEntryBuilderMut.html Access an entry using a hash and a matching function. ```APIDOC ### pub fn from_hash(self, hash: u64, is_match: F) -> RawEntryMut<'a, K, V, S> Access an entry by hash, using a closure to verify the key match. ``` -------------------------------- ### Access IndexMap values by key Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Shows how to access values using the Index trait. Note that indexing with a missing key will cause a panic. ```rust use indexmap::IndexMap; let mut map = IndexMap::new(); for word in "Lorem ipsum dolor sit amet".split_whitespace() { map.insert(word.to_lowercase(), word.to_uppercase()); } assert_eq!(map["lorem"], "LOREM"); assert_eq!(map["ipsum"], "IPSUM"); ``` ```rust use indexmap::IndexMap; let mut map = IndexMap::new(); map.insert("foo", 1); println!("{:?}", map["bar"]); // panics! ``` -------------------------------- ### IndexMap Methods Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Methods for accessing and querying elements in an IndexMap. ```APIDOC ## pub fn contains_key(&self, key: &Q) -> bool ### Description Returns true if an equivalent to key exists in the map. Computes in O(1) time (average). ## pub fn get(&self, key: &Q) -> Option<&V> ### Description Returns a reference to the stored value for key, if it is present, else None. Computes in O(1) time (average). ## pub fn get_key_value(&self, key: &Q) -> Option<(&K, &V)> ### Description Returns references to the stored key-value pair for the lookup key, if it is present, else None. Computes in O(1) time (average). ## pub fn get_full(&self, key: &Q) -> Option<(usize, &K, &V)> ### Description Returns the index with references to the stored key-value pair for the lookup key, if it is present, else None. Computes in O(1) time (average). ## pub fn get_index_of(&self, key: &Q) -> Option ### Description Returns the item index for key, if it is present, else None. Computes in O(1) time (average). ## pub fn get_mut(&mut self, key: &Q) -> Option<&mut V> ### Description Returns a mutable reference to the stored value for key, if it is present, else None. Computes in O(1) time (average). ## pub fn get_key_value_mut(&mut self, key: &Q) -> Option<(&K, &mut V)> ### Description Returns a reference and mutable references to the stored key-value pair for the lookup key, if it is present, else None. Computes in O(1) time (average). ## pub fn get_full_mut(&mut self, key: &Q) -> Option<(usize, &K, &mut V)> ### Description Returns the index with a reference and mutable reference to the stored key-value pair for the lookup key, if it is present, else None. Computes in O(1) time (average). ## pub fn get_disjoint_mut(&mut self, keys: [&Q; N]) -> [Option<&mut V>; N] ### Description Returns the values for N keys. Panics if any key is duplicated. ``` -------------------------------- ### find_any Source: https://docs.rs/indexmap/2.14.0/indexmap/set/rayon/struct.ParIter.html Searches for any item matching the predicate in parallel. ```APIDOC ## fn find_any

(self, predicate: P) -> Option ### Description Searches for some item in the parallel iterator that matches the given predicate and returns it. The item returned may not be the first one in the sequence. ``` -------------------------------- ### Access IndexSet elements by index Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Shows how to access elements by their position in the set. Note that accessing an out-of-bounds index will cause a panic. ```rust use indexmap::IndexSet; let mut set = IndexSet::new(); for word in "Lorem ipsum dolor sit amet".split_whitespace() { set.insert(word.to_string()); } assert_eq!(set[0], "Lorem"); assert_eq!(set[1], "ipsum"); set.reverse(); assert_eq!(set[0], "amet"); assert_eq!(set[1], "sit"); set.sort(); assert_eq!(set[0], "Lorem"); assert_eq!(set[1], "amet"); ``` ```rust use indexmap::IndexSet; let mut set = IndexSet::new(); set.insert("foo"); println!("{:?}", set[10]); // panics! ``` -------------------------------- ### indexmap Macro Definition Source: https://docs.rs/indexmap/2.14.0/indexmap/macro.indexmap.html The macro definition for creating an IndexMap from key-value pairs. Requires the std feature. ```rust macro_rules! indexmap { ($($key:expr => $value:expr,)+) => { ... }; ($($key:expr => $value:expr),*) => { ... }; } ``` -------------------------------- ### IndexSet Inspection Methods Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Methods for querying the state and properties of an IndexSet. ```APIDOC ## pub fn capacity(&self) -> usize ### Description Return the number of elements the set can hold without reallocating. Computes in O(1) time. ## pub fn hasher(&self) -> &S ### Description Return a reference to the set’s `BuildHasher`. ## pub fn len(&self) -> usize ### Description Return the number of elements in the set. Computes in O(1) time. ## pub fn is_empty(&self) -> bool ### Description Returns true if the set contains no elements. Computes in O(1) time. ``` -------------------------------- ### pub fn append(&mut self, other: &mut IndexMap) Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Moves all key-value pairs from another map into the current map. ```APIDOC ## append ### Description Moves all key-value pairs from other into self, leaving other empty. Existing keys in self will have their values updated. ### Parameters - **other** (&mut IndexMap) - The map to move elements from. ``` -------------------------------- ### skip_any Source: https://docs.rs/indexmap/2.14.0/indexmap/map/rayon/struct.ParDrain.html Creates an iterator that skips n elements from anywhere in the original iterator. ```APIDOC ### fn skip_any(self, n: usize) -> SkipAny Creates an iterator that skips `n` elements from anywhere in the original iterator. ``` -------------------------------- ### skip_any Source: https://docs.rs/indexmap/2.14.0/indexmap/map/rayon/struct.IntoParIter.html Creates an iterator that skips n elements from anywhere in the original iterator. ```APIDOC ## fn skip_any(self, n: usize) -> SkipAny ### Description Creates an iterator that skips `n` elements from anywhere in the original iterator. ``` -------------------------------- ### try_reserve Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Attempts to reserve capacity for additional values, returning a Result. ```APIDOC ## pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> ### Description Try to reserve capacity for `additional` more values. Computes in O(n) time. ``` -------------------------------- ### collect Source: https://docs.rs/indexmap/2.14.0/indexmap/set/rayon/struct.ParIter.html Creates a collection from the parallel iterator. ```APIDOC ## fn collect(self) -> C ### Description Creates a fresh collection containing all the elements produced by this parallel iterator. ``` -------------------------------- ### Parallel Sorting Methods Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Methods for sorting IndexSet elements in parallel, requiring the 'rayon' crate feature. ```APIDOC ## pub fn par_sort(&mut self) Sorts the set's values in parallel by their default ordering. ## pub fn par_sort_by(&mut self, cmp: F) Sorts the set's values in place and in parallel using the provided comparison function. ## pub fn par_sort_by_key(&mut self, sort_key: F) Sorts the set's values in place and in parallel using a key extraction function. ## pub fn par_sort_unstable(&mut self) Sorts the set's values in parallel by their default ordering using an unstable sort. ## pub fn par_sort_unstable_by(&mut self, cmp: F) Sorts the set's values in place and in parallel using the provided comparison function (unstable). ## pub fn par_sort_unstable_by_key(&mut self, sort_key: F) Sorts the set's values in place and in parallel using a key extraction function (unstable). ``` -------------------------------- ### serialize Source: https://docs.rs/indexmap/2.14.0/indexmap/map/serde_seq/fn.serialize.html Serializes an IndexMap as an ordered sequence. This function is available when the 'serde' feature is enabled. ```APIDOC ## Function: serialize ### Description Serializes an `IndexMap` as an ordered sequence. This function is intended to be used as a helper for the `serde` derive macro, specifically within the `serialize_with` attribute. ### Signature `pub fn serialize(map: &IndexMap, serializer: T) -> Result` ### Constraints - `K: Serialize` - `V: Serialize` - `T: Serializer` ### Usage Example ```rust #[derive(Serialize)] struct Data { #[serde(serialize_with = "indexmap::map::serde_seq::serialize")] map: IndexMap, } ``` ``` -------------------------------- ### Macro definition for indexmap_with_default Source: https://docs.rs/indexmap/2.14.0/indexmap/macro.indexmap_with_default.html The syntax definition for the indexmap_with_default macro, showing support for key-value pairs with optional trailing commas. ```rust macro_rules! indexmap_with_default { ($H:ty; $($key:expr => $value:expr,)+) => { ... }; ($H:ty; $($key:expr => $value:expr),*) => { ... }; } ``` -------------------------------- ### take_any_while Source: https://docs.rs/indexmap/2.14.0/indexmap/map/rayon/struct.ParDrain.html Creates an iterator that takes elements from anywhere in the original iterator until the predicate returns false. ```APIDOC ### fn take_any_while

(self, predicate: P) -> TakeAnyWhile where P: Fn(&Self::Item) -> bool + Sync + Send Creates an iterator that takes elements from anywhere in the original iterator until the given `predicate` returns `false`. ``` -------------------------------- ### take_any_while Source: https://docs.rs/indexmap/2.14.0/indexmap/map/rayon/struct.IntoParIter.html Creates an iterator that takes elements from anywhere in the original iterator until the predicate returns false. ```APIDOC ## fn take_any_while

(self, predicate: P) -> TakeAnyWhile ### Description Creates an iterator that takes elements from anywhere in the original iterator until the given `predicate` returns `false`. ``` -------------------------------- ### Parallel Execution Configuration Source: https://docs.rs/indexmap/2.14.0/indexmap/map/rayon/struct.IntoParIter.html Methods to configure how the parallel iterator is split across Rayon jobs. ```APIDOC ## Configuration Methods - **with_min_len(self, min: usize) -> MinLen** Sets the minimum length of iterators to process in each Rayon job. - **with_max_len(self, max: usize) -> MaxLen** Sets the maximum length of iterators to process in each Rayon job. ``` -------------------------------- ### serialize Source: https://docs.rs/indexmap/2.14.0/indexmap/map/serde_seq/index.html Serializes an IndexMap as an ordered sequence of (key, value) elements. ```APIDOC ## serialize ### Description Serializes an `IndexMap` as an ordered sequence of `(key, value)` elements instead of a standard map, ensuring order preservation. ### Usage Used within a `#[serde(with = "indexmap::map::serde_seq")]` attribute on a struct field. ``` -------------------------------- ### FromIterator::from_iter Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Constructs an IndexMap from an iterator of key-value pairs. ```APIDOC ## fn from_iter>(iterable: I) -> Self ### Description Creates an `IndexMap` from the sequence of key-value pairs in the provided iterable. This method follows the same logic as `extend`. ``` -------------------------------- ### Define the indexset macro Source: https://docs.rs/indexmap/2.14.0/indexmap/macro.indexset.html The macro definition for indexset, supporting both trailing and non-trailing comma syntax. ```rust macro_rules! indexset { ($($value:expr,)+) => { ... }; ($($value:expr),*) => { ... }; } ``` -------------------------------- ### Using serde_seq with derive macros Source: https://docs.rs/indexmap/2.14.0/indexmap/map/serde_seq/index.html Apply the serde_seq module to a field using the serde(with) attribute to enforce ordered sequence serialization. ```rust #[derive(Deserialize, Serialize)] struct Data { #[serde(with = "indexmap::map::serde_seq")] map: IndexMap, // ... } ``` -------------------------------- ### Index::index (Key Access) Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Accesses a value in the IndexMap using a key. ```APIDOC ## fn index(&self, key: &Q) -> &V ### Description Returns a reference to the value corresponding to the supplied key. Note that this method will panic if the key is not present in the map. ``` -------------------------------- ### append Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Moves all values from another set into this set. ```APIDOC ## pub fn append(&mut self, other: &mut IndexSet) ### Description Moves all values from `other` into `self`, leaving `other` empty. This is equivalent to calling `insert` for each value from `other` in order. ``` -------------------------------- ### Parallel Set Operations Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Methods for performing set comparisons in parallel, requiring the 'rayon' crate feature. ```APIDOC ## pub fn par_is_superset(&self, other: &IndexSet) -> bool ### Description Returns true if all elements of other are contained in self, determined in parallel. ## pub fn par_is_subset(&self, other: &IndexSet) -> bool ### Description Returns true if all elements of self are contained in other, determined in parallel. ``` -------------------------------- ### IndexSet Iteration Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Converting an IndexSet into an iterator or creating one from an iterable. ```APIDOC ## Iteration and Conversion ### Description `IndexSet` implements `IntoIterator` for both owned and referenced instances, allowing for flexible iteration over elements. It also supports `FromIterator` for initialization from existing collections. ### Methods - `into_iter() -> IntoIter`: Consumes the set to create an iterator. - `from_iter(iterable: I) -> Self`: Creates an `IndexSet` from an iterable source. ``` -------------------------------- ### Access IndexMap values by key Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Demonstrates mutable access to values using their associated keys. Index syntax cannot be used to insert new key-value pairs. ```rust use indexmap::IndexMap; let mut map = IndexMap::new(); for word in "Lorem ipsum dolor sit amet".split_whitespace() { map.insert(word.to_lowercase(), word.to_string()); } let lorem = &mut map["lorem"]; assert_eq!(lorem, "Lorem"); lorem.retain(char::is_lowercase); assert_eq!(map["lorem"], "orem"); ``` ```rust use indexmap::IndexMap; let mut map = IndexMap::new(); map.insert("foo", 1); map["bar"] = 1; // panics! ``` -------------------------------- ### Define IndexMap struct Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html The basic structure definition for IndexMap. ```rust pub struct IndexMap { /* private fields */ } ``` -------------------------------- ### map Source: https://docs.rs/indexmap/2.14.0/indexmap/map/rayon/struct.ParIterMut.html Applies a transformation to each item, producing a new iterator. ```APIDOC ## fn map(self, map_op: F) -> Map ### Description Applies `map_op` to each item of this iterator, producing a new iterator with the results. ``` -------------------------------- ### Equivalent Trait Implementation Source: https://docs.rs/indexmap/2.14.0/indexmap/trait.Equivalent.html The blanket implementation for the Equivalent trait using Borrow and Eq. ```rust impl Equivalent for Q where Q: Eq + ?Sized, K: Borrow + ?Sized, ``` -------------------------------- ### Index::index (Range Access) Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Accesses a slice of the IndexMap using range indexing. ```APIDOC ## fn index(&self, range: Range) -> &Slice ### Description Performs the indexing operation using various range types (Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive, or Bound tuples) to return a slice of the map. ``` -------------------------------- ### insert_before Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Inserts a key-value pair before the entry at a specified index or at the end of the map. ```APIDOC ## pub fn insert_before( &mut self, index: usize, key: K, value: V, ) -> (usize, Option) ### Description Inserts a key-value pair before the entry at the given index. If the key exists, it is moved to the new position and the value is updated. ### Parameters - **index** (usize) - The target index for insertion. Must be in range 0..=map.len(). - **key** (K) - The key to insert. - **value** (V) - The value to associate with the key. ### Returns - **(usize, Option)** - The index where the key was placed and the old value if it existed. ``` -------------------------------- ### min Source: https://docs.rs/indexmap/2.14.0/indexmap/map/rayon/struct.ParIter.html Computes the minimum of all items in the iterator. ```APIDOC ## fn min(self) -> Option ### Description Computes the minimum of all the items in the iterator. If the iterator is empty, `None` is returned; otherwise, `Some(min)` is returned. ``` -------------------------------- ### pub fn insert_before(&mut self, index: usize, value: T) -> (usize, bool) Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Inserts a value into the set before the value at the given index, or at the end. If the item exists, it moves the existing value and returns false; otherwise, it inserts the new value and returns true. ```APIDOC ## pub fn insert_before(&mut self, index: usize, value: T) -> (usize, bool) ### Description Inserts the value into the set before the value at the given index, or at the end. If an equivalent item already exists, it moves the original value to the new position and returns false. Otherwise, it inserts the new value at the given index and returns true. ### Parameters - **index** (usize) - The position at which to insert or move the value. Valid indices are 0..=set.len(). - **value** (T) - The value to insert into the set. ### Returns - **(usize, bool)** - A tuple containing the final index of the value and a boolean indicating if a new value was inserted (true) or an existing one was moved (false). ``` -------------------------------- ### binary_search_by Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.Slice.html Searches over a sorted map using a custom comparator function. ```APIDOC ## pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result ### Description Search over a sorted map with a comparator function. Returns the position where that value is present, or the position where it can be inserted to maintain the sort. ``` -------------------------------- ### shrink_to Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Shrinks the capacity of the set with a lower limit. ```APIDOC ## pub fn shrink_to(&mut self, min_capacity: usize) ### Description Shrink the capacity of the set with a lower limit. Computes in O(n) time. ``` -------------------------------- ### reserve_exact Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Reserves capacity for additional values without deliberate over-allocation. ```APIDOC ## pub fn reserve_exact(&mut self, additional: usize) ### Description Reserve capacity for `additional` more values, without over-allocating. Unlike `reserve`, this does not deliberately over-allocate the entry capacity to avoid frequent re-allocations. ``` -------------------------------- ### replace_full Source: https://docs.rs/indexmap/2.14.0/indexmap/set/struct.IndexSet.html Adds a value to the set, replacing the existing value if it is equal to the given one, and returns the index and replaced value. ```APIDOC ## pub fn replace_full(&mut self, value: T) -> (usize, Option) ### Description Adds a value to the set, replacing the existing value, if any, that is equal to the given one, without altering its insertion order. Returns the index of the item and its replaced value. ### Complexity Computes in O(1) time (average). ``` -------------------------------- ### pub fn replace_index(&mut self, index: usize, key: K) -> Result Source: https://docs.rs/indexmap/2.14.0/indexmap/map/struct.IndexMap.html Replaces the key at the specified index. The new key must be unique to the rest of the map. ```APIDOC ## replace_index ### Description Replaces the key at the given index. The new key does not need to be equivalent to the one it is replacing, but it must be unique to the rest of the map. ### Parameters - **index** (usize) - The index of the key to replace. - **key** (K) - The new key to insert. ### Returns - **Result** - Returns Ok(old_key) if successful, or Err((other_index, key)) if an equivalent key already exists. ```