Expand description
§collect_with
A utility crate for enhanced collection operations with capacity control.
§Overview
Provides traits for collecting iterators into collections with:
- Precise capacity management
- Fallible collection operations
- Feature-gated collection types
§Features
§Standard Library Support
std
:- Enables standard library integrations
- When disabled, uses
alloc
crate for no_std environments
§Collection Specialization
collect_vec
:- Enables
CollectVector
trait for enhancedVec
collection - Provides
collect_vec_with()
andcollect_vec_with_exact()
- Enables
ahash
:- Enables
CollectAHash
trait for AHash-powered hash collections - Provides
collect_ahashmap_with()
andcollect_ahashset_with()
- Enables
indexmap
:- Enables
CollectIndex
trait forIndexMap
&IndexSet
collections - Provides
collect_indexmap_with()
andcollect_indexset_with()
- Enables
§Fallible Collection
try
: Enables fallible collectionTryExtract
: Trait for item extraction with error handling, converting fallible types likeOption<T>
toResult<T, ()>
.TryCollectWith
trait for error-propagating collection
§Examples
§Basic usage with collection
use collect_with::CollectWithCapacity;
let numbers = (0..10).collect_with_capacity::<Vec<_>>(20);
assert_eq!(numbers.capacity(), 20);
use collect_with::CollectWith;
let s = [vec!["a"], vec!["b", "c", "d"]]
.into_iter()
.flatten()
.collect_with::<String>(|size| match size {
0 => 8,
n => n,
});
assert_eq!(s.len(), 4);
assert_eq!(s.capacity(), 8);
use collect_with::CollectVector;
let numbers = (0..10).collect_vec_with(|hint|{
match hint {
0 => 12,
n => n + 5,
}
});
assert_eq!(numbers.capacity(), 15);
§Fallible collection (requires try
feature)
let result = [Some(12), Some(42), Some(77)]
.into_iter()
.try_collect_vec_with(|u| u); // Result<Vec<i32>, ()>
assert_eq!(result.as_deref(), Ok(&[12, 42, 77][..]));
// -----
use collect_with::{TryCollectWith, TryExtract};
let result = ["42", "76", "abc"]
.into_iter()
.map(|x| x.parse::<i32>()) // &str -> Result<i32>
.try_collect_with::<Vec<_>, _, _>(|u| u + 3); // -> Result<Vec<i32>, ParseIntError>
assert!(result.is_err());
§About the Final Capacity Size
For example, (0..10).collect_vec_with(|_size_bound| 2)
(0..10).size_hint()
returns(10, Some(10))
.
- lower_bound = 10 => lower
- upper_bound = Some(10) => upper
max(lower, upper.unwrap_or(lower))
=> 10
- _size_bound is 10.
- The closure returns 2
- The final capacity is
max(_size_bound, 2)
=>max(10, 2)
= 10
- The vector is created with
Vec::with_capacity(10)
, instead ofVec::with_capacity(2)
.
If you need an exact capacity size, please use the .collect_with_exact()
or .collect_vec_with_exact()
§Traits
§Core Components
ExtendWithCapacity
: Base trait for capacity-aware collectionsCollectWith
/CollectWithCapacity
: Primary collection traits
§Optional Components
CollectVector
(feature = “collect_vec”): Specialized Vec collection methodsCollectAHash
(feature = “ahash”): AHash-based collection supportCollectIndex
(feature = “indexmap”): IndexMap/IndexSet collection supportTryExtract
/TryCollectWith
(feature = “try”)
Traits§
- CollectA
Hash ahash
- Trait for collecting items into AHashMap or AHashSet with a specified capacity.
- Collect
Index indexmap
- Trait for collecting items into IndexMap or IndexSet with specified capacity while preserving insertion order.
- Collect
Vector collect_vec
- Trait providing enhanced vector collection strategies for iterators.
- Collect
With - Trait for collecting iterator elements with flexible capacity calculation
- Collect
With Capacity - Trait for collecting iterator elements into a collection with specified capacity
- Extend
With Capacity - A trait for collections that can be pre-allocated with specific capacity and extended with elements.
- TryCollect
With try
- TryExtract
try
- A trait for uniformly extracting success/error values from various container types.