Several ideas about formatting

  1. reusable format_template!({string literal}) (how to resolve named parameter needed to be captured?)

  2. delayed format intermediate like chrono::format::DelayedFormat

if using format_args! like this

let s = "abc";
let n = 42;
let x = format_args!("test {s} {n}");
println!("Hello, world! {}", x);

error temporary value dropped while borrowed will be raised.

  1. std::fmt::write_to_string specialization similar to std::fmt::write without std::io::Error

Internally, format_args! is constructed in a way that prevents it from being assigned to a variable (it creates temporary arrays that don't live long enough).

It's not the temporary arrays that are the issue, it's the function calls blocking lifetime extension of those arrays. I've re-implemented format_args! as a macro but using struct-constructor syntax instead of function calls and it allows this sort of binding just fine.

2 Likes