Expand description
Practical extension methods for Rust standard library collections.
Enables direct functional-style collection manipulation without the usual iterator boilerplate.
§Features
- Equivalents of standard iterator methods are added to standard library collections
- Additional utility methods commonly found in collection libraries are also included
- Transformation methods return a new collection instance instead of returning an iterator
- All methods treat collection instances as immutable although some may consume them
- Performance is near optimal and overhead is limited to new collection creation
§Examples
use cantrip::*;
let a = vec![1, 2, 3];
a.fold(0, |r, x| r + x); // 6
a.map_ref(|&x| (x, x + 1)).to_map(); // HashMap::from([(1, 2), (2, 3), (3, 4)])
a.flat_map(|x| [x, -x]).sorted(); // vec![-3, -2, -1, 1, 2, 3]
a.filter(|&x| x > 1).into_set(); // HashSet::from([2, 3])
a.group_by(|x| x % 2); // HashMap::from([(0, vec![2]), (1, vec![1, 3])])
a.delete(&1).add(2).unique(); // vec![2, 3]
a.substitute_at(0, 4).to_list(); // LinkedList::from([4, 2, 3])
a.position_multi(|&x| x % 2 == 1); // vec![0, 2]
a.rev().into_iter().into_deque(); // VecDeque::from([3, 2, 1])§Methods
Traits§
- Non-consuming collection operations.
- Consuming collection operations.
- Conversion into an
Iteratorover references. - List operations.
- Map operations.
- Ordered collection operations.
- Sequence operations.
- Slice operations.
- Non-consuming transform operations.
- Consuming transform operations.
- Vector transform operations.
- Consuming vector transform operations.