#display #string #text #str

slicedisplay

Simplistic Display implementation for Vecs and slices

4 releases

0.2.2 Aug 25, 2022
0.2.1 Jul 19, 2022
0.2.0 May 28, 2022
0.1.0 May 27, 2022

#1033 in Text processing

Download history 105/week @ 2024-11-15 61/week @ 2024-11-22 77/week @ 2024-11-29 147/week @ 2024-12-06 203/week @ 2024-12-13 80/week @ 2024-12-20 37/week @ 2024-12-27 77/week @ 2025-01-03 87/week @ 2025-01-10 229/week @ 2025-01-17 66/week @ 2025-01-24 48/week @ 2025-01-31 133/week @ 2025-02-07 145/week @ 2025-02-14 165/week @ 2025-02-21 84/week @ 2025-02-28

552 downloads per month
Used in 2 crates

MIT/Apache

8KB
119 lines

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)"
);

No runtime deps