Pencil-Box
A simple and efficient Rust utility for:
- ๐ฆ Splitting slices into fixed-size chunks with
chunk - ๐ Removing duplicate elements from vectors with
uniqanduniq_performant - โ Computing set differences between vectors with
differenceanddifference_performant - ๐๏ธ Removing "empty" elements from vectors with
compact
๐ฆ Installation
Add this to your Cargo.toml:
[]
= "0.1.6"
Replace
"0.1.6"with the latest version from crates.io
๐ Usage
โ๏ธ Chunking Slices
use chunk;
chunk Function Behavior
- โ
Returns an error if
chunk_size == 0 - โ Returns an empty vector if the input slice is empty
- โ
Returns a single chunk if
chunk_size >= array.len() - โ
Returns multiple chunks of up to
chunk_sizeelements otherwise
Each chunk is cloned into an owned Vec<T>.
๐ Deduplicating Vectors
use ;
uniq Function Behavior
- โ
Uses the standard
HashSetto remove duplicates in-place - โ Retains the first occurrence of each element
- โ Preserves relative ordering
- ๐ Best for general-purpose use
uniq_performant Function Behavior
- ๐ Uses
AHashSetfor faster hashing and performance - โ Retains the first occurrence of each element
- โ Preserves relative ordering
- โก Ideal for high-throughput or performance-critical use cases
โ Computing Vector Differences
use ;
difference Function Behavior
- โ
Returns a new
Vec<T>excluding any elements found inothers - โ
Uses standard
HashSetfor equality and lookup - โ Clones values into the output (fully owned)
- โ Best for secure, general-purpose usage
- โก Preallocates memory for performance
difference_performant Function Behavior
- ๐ Uses
AHashSetfor faster lookup performance - โ
Identical output to
difference - โ Clones values for full ownership
- โ ๏ธ Not cryptographically secure (faster but non-resistant to hash DoS attacks)
- โก Best for large or performance-critical data sets
๐๏ธ Compacting Vectors
use compact;
compact Function Behavior
- ๐๏ธ Removes elements from a mutable vector that are considered "empty"
- โ Modifies the vector in-place
- โ
Uses the
IsEmptytrait for custom emptiness logic - โ
Works with
String,&str,Vec<T>,Option<T>,bool, integers, and floats - โก Efficient: uses
Vec::retain()under the hood
๐งช โ Running Tests
To run all unit tests
cargo test --tests
๐ Safety
- โ
100% safe Rust (
#![forbid(unsafe_code)]) - โ
No
unsafeblocks used - โ Pure functional logic
๐ License
๐ค Contributing
Contributions, bug reports, and feature requests are welcome.
Please open an issue or submit a pull request.