array
Helpers for slices and Vecs that the standard library does not provide.
Inputs are borrowed slices and results are new Vecs: nothing is mutated. Membership-based
helpers require Eq + Hash. Shuffling and sampling live in the random module, where the
generator is an argument.
Install
Section titled “Install”Cargo feature array (enabled by default). To compile only this module:
or in Cargo.toml:
Import path: helpers4::array.
| Item | What it does |
|---|---|
cartesian_product | Returns every pair (x, y) with x from a and y from b, in row-major order. |
count_by | Counts the elements of items per key returned by key. |
difference | Returns the elements of a that are not in b, in a’s order. |
duplicates | Returns the elements that appear more than once in items, each one once, in the order they first appear. |
equals_unordered | Returns true when a and b hold the same elements the same number of times, in any order. |
group_by | Groups the elements of items by the key returned by key. |
interleave | Alternates the elements of a and b, starting with a; the leftover of the longer slice goes at the end. |
intersection | Returns the elements of a that also appear in b, in a’s order. |
intersects | Returns true when a and b share at least one element. |
key_by | Indexes the elements of items by the key returned by key. |
replace_or_append | Replaces the first element that matches with item, or appends item when none does: the “upsert into a list” gesture. |
symmetric_difference | Returns the elements present in exactly one of a and b. |
toggle | Removes item from items if it is there, adds it at the end if it is not: the usual “toggle a selection” gesture. |
toggle_by | Like toggle, but elements are the same when key gives the same value: toggle objects by id instead of by whole-value equality. |
unique | Removes duplicate values, keeping the first occurrence of each and the original order. |
unique_by | Removes elements whose key was already seen, keeping the first of each key in order. |
