Elegance
A pretty-printing library for Rust with a focus on speed and compactness.
Usage
Create a printer:
let mut pp = new;
Add some text and spaces:
pp.text?;
pp.space?; // breakable space
pp.hard_break?; // forced line break
Enclose structures in groups:
pp.group?;
Finish the document:
let result = pp.finish?;
println!;
Streaming output
The printer can write to any std::io::Write implementation.
use ;
let mut pp = new;
Examples
use *;
Differences from other libraries
This crate implements an Oppen-style pretty-printing library, while the pretty crate follows a Walder-style approach.
In Walder-style pretty-printing, documents are constructed using a composable Doc type and combinators. Here's an example:
This method is particularly suitable for functional programming languages but may not be ideal for Rust. Converting a syntax tree into a Doc requires additional memory allocation proportional to the size of the entire document.
The key difference with this library is that it represents the structure of the printed document through control flow rather than data structures. As a result, the printing process is fully streamed and operates within a constant memory footprint.
References
- Oppen, Dereck C. "Prettyprinting." ACM Transactions on Programming Languages and Systems (TOPLAS) 2.4 (1980): 465-483. https://doi.org/10.1145/357114.357115
- Swierstra, S. Doaitse, and Olaf Chitil. "Linear, bounded, functional pretty-printing." Journal of Functional Programming 19.1 (2009): 1-16. https://doi.org/10.1017/s0956796808006990