Crate cantrip

source ·
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

Method / Collection typeVec, VecDeque, LinkedListSliceHashSet, BTreeSet, BinaryHeapHashMap, BTreeMapConsuming
add***Y
add_at*Y
add_at_multi*Y
add_multi***Y
all****N
any****N
cartesian_product*N
chunked*Y
chunked_by*Y
chunked_exact*Y
combinations**N
combinations_multi*N
coalesce*Y
collect****N
collect_to****Y
common_prefix_length**N
common_suffix_length**N
count_by****N
count_unique***N
delete***Y
delete_at*Y
delete_at_multi*Y
delete_multi***Y
divide*Y
divide_by*Y
disjoint****N
duplicates*Y
duplicates_by*Y
enumerate*Y
equivalent**N
fill*Y
fill_with***Y
filter***Y
filter_keys*Y
filter_map***Y
filter_map_ref***N
filter_values*Y
find****N
find_map***Y
find_map_ref****N
find_position**N
first**N
flat_map***Y
flat_map_ref***N
flat**Y
fold***Y
fold_ref****N
for_each****N
frequencies**N
frequencies_by**N
group_by**Y
group_fold**Y
group_fold_ref***N
group_reduce**Y
group_reduce_ref***N
init**Y
interleave*Y
interleave_exact*Y
intersect***Y
intersperse*Y
intersperse_with*Y
into_bmap***Y
into_bset***Y
into_heap***Y
into_list***Y
into_map***Y
into_set***Y
into_vec****Y
into_deque***Y
joined*N
largest**Y
last*N
map***Y
map_ref***N
map_keys*Y
map_values*Y
map_while*N
max_by****N
max_by_key****N
max_of****N
merge*Y
merge_by*Y
min_by****N
min_by_key****N
min_of****N
minmax_by****N
minmax_by_key****N
minmax_of****N
move_at*Y
pad_left*Y
pad_left_with*Y
pad_right*Y
pad_right_with*Y
partition***Y
partition_map***Y
partition_map_ref***N
position**N
position_multi**N
position_of**N
position_of_multi**N
position_sequence**N
powerset**N
product**Y
product_keys*Y
product_values*Y
reduce***Y
reduce_ref****N
repeat*
rev*Y
rfind**N
rfold*Y
rfold_ref**N
rposition**N
scan*Y
scan_ref*N
skip**Y
skip_while**Y
slice*Y
smallest**Y
sorted*Y
sorted_by*Y
sorted_by_cached_key*Y
sorted_by_key*Y
sorted_unstable*Y
sorted_unstable_by*Y
sorted_unstable_by_key*Y
step_by*Y
subset****N
substitute***Y
substitute_at*Y
substitute_at_multi*Y
substitute_multi***Y
superset****N
sum**Y
sum_keys*Y
sum_values*Y
swap_at*Y
tail**Y
take**Y
take_while**Y
unique*Y
unique_by*Y
unit***Y
unzip*Y
to_bmap****N
to_bset****N
to_heap****N
to_keys*N
to_list****N
to_map****N
to_set****N
to_values*N
to_vec****N
to_deque****N
variations*N
windowed*N
windowed_circular*N
zip*Y
zip_padded*Y

Traits§