pub fn display_with(f: impl Fn(&mut Formatter<'_>) -> Result) -> impl DisplayExpand description
Helper function to defer formatting to later, without having to allocate a intermediate String
similar to format_args!, but it can be returned by moved values
Example:
// instead of `fn nested() -> String`
fn nested() -> impl Display {
let new_string = String::from("Hello allocated string");
// instead of `format!("Formatted! {}", new_string)`
display_with(move |f| write!(f, "Formatted! {}", new_string))
}
println!("No Extra allocation:\n{}", nested());