Literator: Iterator formatting for literate programmers
This crate provides the Literator trait, which provides utilities for
efficiently displaying the items of an iterator without temporary allocations.
Example use cases
Efficient, allocation-free string concatenation:
# use Literator;
let favorite_things = ;
let joined = favorite_things.iter.join; // Does not allocate
let message = joined.to_string; // ... unless asked.
assert_eq!;
Some things require special display logic that cannot be represented as slices without allocation, like paths:
# use Literator;
# use Path;
let paths = ;
let list = paths.iter.map.join.to_string;
assert_eq!;
Friendlier error messages with ad-hoc formatting:
# use Literator;
assert_eq!;
Features
- Allocation-free. Custom
Debug/Displayimplementations for various adapters render directly to the output stream. - General
join(). The standard library only hasstd::slice::join(), which heap allocates and only works for slices, not iterators.Itertools::join()only works forDisplay, notDebug, and only supports using a&stras the delimiter. - General
oxford_join()for listing items with appropriate punctuation. Theoxford_joincrate, while great, only works on slices and collections, heap allocates the result, and only supports string conjunctions.Literator::oxford_join_custom()supports using any displayable delimiter, including non-English conjunctions. - Prefix/suffix/surround each item: Adapters for adding prefixes/suffixes to the items of an iterator while displaying them.
- Adapters forward all formatting options to the item being displayed. For
example,
println!("{:.02}", [1.0, 2.0].iter().join(", "))prints"1.00, 2.00". - Zero dependencies.
#![no_std]#![forbid(unsafe_code)]