pencil-box 0.1.2

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 to split slices into fixed-size chunks, returning owned Vec<T> chunks.


๐Ÿ“ฆ Installation

Add this to your Cargo.toml:

[dependencies]
pencil-box = "0.1.1"

Replace "0.1.0" with the latest version from crates.io


๐Ÿš€ Usage

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

๐Ÿงฉ Function Behavior

pub fn chunk<T: Clone>(array: &[T], chunk_size: usize) -> Result<Vec<Vec<T>>, &'static str>

The chunk function behaves as follows:

  • โœ… Returns an error if chunk_size == 0
  • โœ… Returns an empty vector if the input slice is empty
  • โœ… Returns a single chunk containing all elements if chunk_size >= array.len()
  • โœ… Returns multiple chunks of up to chunk_size elements otherwise

Each chunk is a cloned Vec<T>, preserving data integrity and independence.


๐Ÿงช Test Coverage

The implementation is covered by comprehensive unit tests, including:

  • โœ… Primitive types: i32, bool, etc.
  • โœ… Strings and owned String
  • โœ… Structs (Clone + PartialEq)
  • โœ… Enums (e.g., Status::Ok, Status::Error)
  • โœ… Nested collections (e.g., Vec<Vec<T>>)
  • โœ… Edge cases:
    • Empty input
    • chunk_size == 0 (returns error)
    • chunk_size >= input.len() (returns single chunk)

To run tests:

cargo test

๐Ÿ”’ 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