slicedisplay

Crates.ioslicedisplay
lib.rsslicedisplay
version0.2.2
sourcesrc
created_at2022-05-27 01:43:18.561268
updated_at2022-08-25 15:50:03.703401
descriptionSimplistic Display implementation for Vecs and slices
homepagehttps://github.com/vrmiguel/slicedisplay
repositoryhttps://github.com/vrmiguel/slicedisplay
max_upload_size
id594704
size19,624
Vinícius Miguel (vrmiguel)

documentation

https://docs.rs/slicedisplay

README

slicedisplay - lightweight Display for Vecs and slices

slicedisplay is a tiny no-std crate which supplies the SliceDisplay trait.

This trait extends AsRef<[T]> with the display method, which allows formatting without heap allocations.

Requires at least Rust 1.58.

Usage

use slicedisplay::SliceDisplay;

let empty: Vec<u8> = Vec::new();
assert_eq!(empty.display().to_string(), "[]");

let single = Vec::from([1]);
assert_eq!(single.display().to_string(), "[1]");

let numbers = Vec::from([1, 2, 3, 4, 5]);
assert_eq!(numbers.display().to_string(), "[1, 2, 3, 4, 5]");

It's also possible to slightly customize the display.

use slicedisplay::SliceDisplay;

let hello: Vec<_> = "Hello".chars().collect();
assert_eq!(
    hello.display().delimiter(';').to_string(),
    "[H; e; l; l; o]"
);
assert_eq!(
    hello.display().terminator('{', '}').to_string(),
    "{H, e, l, l, o}"
);
assert_eq!(
    hello
        .display()
        .terminator('(', ')')
        .delimiter(';')
        .to_string(),
    "(H; e; l; l; o)"
);

assert_eq!(
    hello
        .display()
        .terminator('(', ')')
        .delimiter(';')
        .should_space(false)
        .to_string(),
    "(H;e;l;l;o)"
);
Commit count: 10

cargo fmt