Expand description
A simple crate that provides a way to create instances of opaque types that implement
fmt::Display
and fmt::Debug
by calling a provided formatting closure.
§Examples
use display_with::{display_with, debug_with};
let display = display_with(|f| write!(f, "Hello, world!"));
assert_eq!(format!("{display}"), "Hello, world!");
let debug = debug_with(|f| write!(f, "Hello, world!"));
assert_eq!(format!("{debug:?}"), "Hello, world!");
This can be combined with the format_args!
macro to use the opaque types with the write!
and
writeln!
macros.
use core::fmt::Write;
use display_with::{display_with, debug_with};
fn main() -> std::fmt::Result {
let display = display_with(|f| write!(f, "Hello, world!"));
let mut s = String::new();
// Unlike `s.push_str(&format!("{display}"))`, this doesn't require an extra allocation.
write!(&mut s, "{}", format_args!("{display}"))?;
Ok(())
}
Credit: https://internals.rust-lang.org/t/format-args-with-long-lifetimes/19494/2.
Functions§
- debug_
with - Creates an instance of an opaque type that implements
fmt::Debug
by calling the provided formatting closure. - display_
with - Creates an instance of an opaque type that implements
fmt::Display
by calling the provided formatting closure.