3 releases
Uses new Rust 2024
| 0.0.2 | Mar 4, 2025 |
|---|---|
| 0.0.1 | Feb 26, 2025 |
| 0.0.0 | Feb 26, 2025 |
#936 in Rust patterns
130 downloads per month
Used in 6 crates
(2 directly)
37KB
492 lines
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
alloccrate for no_std environments
Collection Specialization
collect_vec:- Enables
CollectVectortrait for enhancedVeccollection - Provides
collect_vec_with()andcollect_vec_with_exact()
- Enables
ahash:- Enables
CollectAHashtrait for AHash-powered hash collections - Provides
collect_ahashmap_with()andcollect_ahashset_with()
- Enables
indexmap:- Enables
CollectIndextrait forIndexMap&IndexSetcollections - 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, ()>.TryCollectWithtrait for error-propagating collection
Examples
Basic usage with collection
collect_with_capacity
use collect_with::CollectWithCapacity;
let numbers = (0..10).collect_with_capacity::<Vec<_>>(20);
assert_eq!(numbers.capacity(), 20);
collect_with closure
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);
collect_vec_with
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)
use collect_with::{TryCollectWith, TryExtract};
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());
collect indexmap (requires indexmap feature)
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);
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: A trait for collections that can be pre-allocated with specific capacity and extended with elements.CollectWith/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")
Dependencies
~0–375KB