Crate lazy_format[][src]

Expand description

lazy_format is a collection of format!-style macros which lazily formatting their arguments 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 obviously profoundly unsafe 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 {
    lazy_format!("<{tag}>{content}</{tag}>", tag=tag, content=content)

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



horrorshow is a macro-based HTML templating library. If the horrorshow feature is enabled, lazy_format objects will implement horrorshow::Render, allowing them to be used in horrorshow templates. The formatted content will be HTML-escaped by horrorshow.



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.

Lazily format something by eagerly evaluate the arguments ahead of time, then storing them and formatting them at write time.