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 -
๐๏ธ Removing "empty" elements from vectors with
compact
๐ฆ Installation
Add this to your Cargo.toml:
[dependencies]
pencil-box = "0.1.4"
Replace
"0.1.4"with the latest version from crates.io
๐ Usage
โ๏ธ Chunking Slices
use pencil_box::chunk;
fn main() {
let data = vec![1, 2, 3, 4, 5];
let result = chunk(&data, 2).unwrap();
assert_eq!(result, vec![vec![1, 2], vec![3, 4], vec![5]]);
}
chunk Function Behavior
pub fn chunk<T: Clone>(array: &[T], chunk_size: usize) -> Result<Vec<Vec<T>>, &'static str>
-
โ 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 pencil_box::{uniq, uniq_performant};
fn main() {
let mut items = vec![1, 2, 2, 3, 1];
uniq(&mut items); // or uniq_performant(&mut items);
assert_eq!(items, vec![1, 2, 3]);
}
uniq Function Behavior
pub fn uniq<T: Eq + Hash + Clone>(values: &mut Vec<T>)
-
โ 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
pub fn uniq_performant<T: Eq + Hash + Clone>(values: &mut Vec<T>)
-
๐ 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
๐๏ธ Compacting Vectors
use pencil_box::compact; // Assuming compact is part of pencil_box
fn main() {
let mut numbers = vec![1, 0, 2, 0, 3];
compact(&mut numbers);
assert_eq!(numbers, vec![1, 2, 3]);
let mut strings = vec!["hello".to_string(), "".to_string(), "world".to_string()];
compact(&mut strings);
assert_eq!(strings, vec!["hello".to_string(), "world".to_string()]);
}
compact Function Behavior
pub fn compact<T: IsEmpty>(values: &mut Vec<T>)
-
๐๏ธ Removes elements from a mutable vector that are considered "empty"
-
โ Modifies the vector in-place
-
โ Relies on the
IsEmptytrait to determine emptiness -
Supported types for
IsEmptyincludeString,&str,Vec<T>,bool, all integer types (i8,u8, etc.), all float types (f32,f64), andOption<T>. -
โก Efficient: Uses
Vec::retain()for in-place removal.
๐ 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.