| Crates.io | collect-with |
| lib.rs | collect-with |
| version | 0.0.2 |
| created_at | 2025-02-26 13:47:16.140686+00 |
| updated_at | 2025-03-04 14:16:59.049149+00 |
| description | A utility crate for enhanced collection operations with capacity control. |
| homepage | |
| repository | https://github.com/2moe/collect-with |
| max_upload_size | |
| id | 1570471 |
| size | 56,791 |
A utility crate for enhanced collection operations with capacity control.
Provides traits for collecting iterators into collections with:
std:
alloc crate for no_std environmentscollect_vec:
CollectVector trait for enhanced Vec collectioncollect_vec_with() and collect_vec_with_exact()ahash:
CollectAHash trait for AHash-powered hash collectionscollect_ahashmap_with() and collect_ahashset_with()indexmap:
CollectIndex trait for IndexMap & IndexSet collectionscollect_indexmap_with() and collect_indexset_with()try: Enables fallible collection
TryExtract: Trait for item extraction with error handling,
converting fallible types like Option<T> to Result<T, ()>.TryCollectWith trait for error-propagating collectionuse 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);
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());
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);
For example, (0..10).collect_vec_with(|_size_bound| 2)
(0..10).size_hint() returns (10, Some(10)).max(lower, upper.unwrap_or(lower)) => 10max(_size_bound, 2) => max(10, 2) = 10Vec::with_capacity(10), instead of Vec::with_capacity(2).If you need an exact capacity size, please use the .collect_with_exact() or .collect_vec_with_exact()
ExtendWithCapacity: A trait for collections that can be pre-allocated with specific capacity and extended with elements.CollectWith/CollectWithCapacity: Primary collection traitsCollectVector (feature = "collect_vec"): Specialized Vec collection methodsCollectAHash (feature = "ahash"): AHash-based collection supportCollectIndex (feature = "indexmap"): IndexMap/IndexSet collection supportTryExtract/TryCollectWith (feature = "try")