#lazy #format #pattern #lazy-evaluation

no-std lazy_format

A utility crate for lazily formatting values for later

22 stable releases

2.0.3 Nov 29, 2023
2.0.0 Aug 11, 2022
1.10.0 Dec 5, 2021
1.9.0 Aug 21, 2021
1.2.1 Feb 16, 2019

#125 in Rust patterns

Download history 19610/week @ 2023-10-27 18953/week @ 2023-11-03 26181/week @ 2023-11-10 24965/week @ 2023-11-17 15923/week @ 2023-11-24 18383/week @ 2023-12-01 15561/week @ 2023-12-08 11785/week @ 2023-12-15 3464/week @ 2023-12-22 10222/week @ 2023-12-29 16595/week @ 2024-01-05 16981/week @ 2024-01-12 20322/week @ 2024-01-19 20750/week @ 2024-01-26 14307/week @ 2024-02-02 20410/week @ 2024-02-09

79,421 downloads per month
Used in 31 crates (12 directly)

MPL-2.0 license

20KB
150 lines

Travis (.com) Crates.io docs.rs license

lazy_format

A [no_std] library for lazily formatting things. Because allocating temporary strings just to write them to a buffered stream is bad for your health.

use std::io;

use lazy_format::lazy_format;
use joinery::JoinableIterator;

fn main() {
	let result = (0..10)
		.map(|value| lazy_format!("\t'{}'", value))
		.join_with("\n")
		.to_string();

	assert_eq!(result,
"	'0'
	'1'
	'2'
	'3'
	'4'
	'5'
	'6'
	'7'
	'8'
	'9'")
}

The above example is the key motivating example: when building up some kind of object you wish to write or format, there's no reason to allocate intermediary strings (which is what format! does). Instead, lazy_format! 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).

No runtime deps