Macro lazy_format::make_lazy_format[][src]

macro_rules! make_lazy_format {
    ($fmt : ident => $write : expr) => { ... };
}
Expand description

Low level constructor for lazy format instances. Create a lazy formatter with a custom closure as its Display implementation, for complete control over formatting behavior at write time.

make_lazy_format! is the low-level constructor for lazy format instances. It is completely customizable, insofar as it allows you to create a custom Display::fmt implementation at the call site.

make_lazy_format! takes a closure specification as an argument, and creates a Display struct that captures the local environment in a closure and uses it as the formatting function.

Example:

use std::fmt::Display;
use lazy_format::make_lazy_format;

let data = vec![1, 2, 3, 4, 5];

let comma_separated = make_lazy_format!(f => {
    let mut iter = data.iter();
    match iter.next() {
        None => Ok(()),
        Some(first) => {
            write!(f, "{}", first)?;
            iter.try_for_each(|value| write!(f, ", {}", value))
        }
    }
});

let result = comma_separated.to_string();

assert_eq!(result, "1, 2, 3, 4, 5");