pencil-box 0.1.4

A versatile Rust utility library, inspired by JavaScript's Lodash, providing common helper functions for collections, strings, numbers, and more, with no external dependencies.
Documentation

Pencil-Box

A simple and efficient Rust utility for:

  • ๐Ÿ“ฆ Splitting slices into fixed-size chunks with chunk

  • ๐Ÿ” Removing duplicate elements from vectors with uniq and uniq_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_size elements 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 HashSet to 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 AHashSet for 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 IsEmpty trait to determine emptiness

  • Supported types for IsEmpty include String, &str, Vec<T>, bool, all integer types (i8, u8, etc.), all float types (f32, f64), and Option<T>.

  • โšก Efficient: Uses Vec::retain() for in-place removal.

๐Ÿ”’ Safety

  • 100% safe Rust (#![forbid(unsafe_code)])

  • No unsafe blocks used

  • Pure functional logic

๐Ÿ“„ License

๐Ÿค Contributing

Contributions, bug reports, and feature requests are welcome. Please open an issue or submit a pull request.

๐ŸŒ Links