pub trait CollectIndex: Iterator {
// Provided methods
fn collect_indexmap_with<K, V>(
self,
capacity: impl FnOnce(usize) -> usize,
) -> IndexMap<K, V, RandomState>
where Self: Sized + Iterator<Item = (K, V)>,
K: Hash + Eq { ... }
fn collect_indexmap_with_exact<K, V>(
self,
capacity: impl FnOnce(usize) -> usize,
) -> IndexMap<K, V>
where Self: Sized + Iterator<Item = (K, V)>,
K: Hash + Eq { ... }
fn collect_indexset_with<K>(
self,
capacity: impl FnOnce(usize) -> usize,
) -> IndexSet<K, RandomState>
where Self: Sized + Iterator<Item = K>,
K: Hash + Eq { ... }
fn collect_indexset_with_exact<K>(
self,
capacity: impl FnOnce(usize) -> usize,
) -> IndexSet<K>
where Self: Sized + Iterator<Item = K>,
K: Hash + Eq { ... }
}Available on crate feature
indexmap only.Expand description
Trait for collecting items into IndexMap or IndexSet with specified capacity while preserving insertion order.
Provided Methods§
Sourcefn collect_indexmap_with<K, V>(
self,
capacity: impl FnOnce(usize) -> usize,
) -> IndexMap<K, V, RandomState>
Available on crate feature ahash only.
fn collect_indexmap_with<K, V>( self, capacity: impl FnOnce(usize) -> usize, ) -> IndexMap<K, V, RandomState>
ahash only.Collects items into an IndexMap<K, V, ahash::RandomState> with a
specified capacity.
§Example
use indexmap::IndexMap;
use collect_with::CollectIndex;
let map = ('a'..='i')
.zip(100..=109)
.collect_indexmap_with(|u| u + 1); // u + 1 => 9 + 1 = 10
assert_eq!(map.get(&'a'), Some(&100));
assert_eq!(map.get_index(0), Some((&'a', &100)));
assert_eq!(map.get_index(2), Some((&'c', &102)));
assert_eq!(map.capacity(), 10);Sourcefn collect_indexmap_with_exact<K, V>(
self,
capacity: impl FnOnce(usize) -> usize,
) -> IndexMap<K, V>
fn collect_indexmap_with_exact<K, V>( self, capacity: impl FnOnce(usize) -> usize, ) -> IndexMap<K, V>
Collects items into an IndexMap with exact specified capacity.
Preserves insertion order and strictly uses calculated capacity.
§Example
use indexmap::IndexMap;
use collect_with::CollectIndex;
let map = [(1, "a"), (2, "b"), (3, "c")]
.into_iter()
.collect_indexmap_with_exact(|size_hint| size_hint);
assert_eq!(map.len(), 3);
assert_eq!(map.capacity(), 3);Sourcefn collect_indexset_with<K>(
self,
capacity: impl FnOnce(usize) -> usize,
) -> IndexSet<K, RandomState>
Available on crate feature ahash only.
fn collect_indexset_with<K>( self, capacity: impl FnOnce(usize) -> usize, ) -> IndexSet<K, RandomState>
ahash only.Collects items into an IndexSet<K, ahash::RandomState> with specified
capacity.
Preserves insertion order and maintains unique elements.
§Example
use indexmap::IndexSet;
use collect_with::CollectIndex;
let set = (0..3)
.collect_indexset_with(|size_hint| size_hint + 2);
assert_eq!(set.len(), 3);
assert!(set.capacity() >= 3);Sourcefn collect_indexset_with_exact<K>(
self,
capacity: impl FnOnce(usize) -> usize,
) -> IndexSet<K>
fn collect_indexset_with_exact<K>( self, capacity: impl FnOnce(usize) -> usize, ) -> IndexSet<K>
Collects items into an IndexSet with exact specified capacity.
Preserves insertion order and strictly uses calculated capacity.
§Example
use indexmap::IndexSet;
use collect_with::CollectIndex;
let set = (0..3)
.into_iter()
.collect_indexset_with_exact(|size_hint| size_hint);
assert_eq!(set.len(), 3);
assert_eq!(set.capacity(), 3);