pencil-box 0.1.3

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

๐Ÿ“ฆ Installation

Add this to your Cargo.toml:

[dependencies]
pencil-box = "0.1.2"

Replace "0.1.2" 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]]);
}

๐Ÿงน 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]);
}

๐Ÿงฉ Function Behavior

chunk

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>.


uniq

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

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

๐Ÿ”’ Safety

  • 100% safe Rust (#![forbid(unsafe_code)])
  • No unsafe blocks used
  • Pure functional logic

๐Ÿ“„ License

This project is dual-licensed under either:

You may freely choose either license.


๐Ÿค Contributing

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


๐ŸŒ Links