Crate lazy_format

source ·
Expand description

lazy_format! is a format!-style macro which lazily formats its arguments. That is, rather than immediatly formatting them into a String (which is what format! does), it captures its arguments and returns an opaque struct with a Display implementation, so that the actual formatting can happen directly into its final destination buffer (such as a file or string).

use std::fmt::Display;

use lazy_format::prelude::*;

// NOTE: This is profoundly insecure and you should never actually
// render HTML without escape guards, code injection prevention, etc.
fn html_tag<'a>(tag: &'a str, content: impl Display + 'a) -> impl Display + 'a {

let result = html_tag("div", html_tag("p", "Hello, World!")).to_string();
assert_eq!(result, "<div><p>Hello, World!</p></div>");



  • Lazily format something. Essentially the same as format!, except that instead of formatting its arguments to a string, it captures them in an opaque struct, which can be formatted later. This allows you to build up formatting operations without any intermediary allocations or extra formatting calls. Also supports lazy conditional and looping constructs.
  • 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.