use itertools::Itertools;
fn main() {
let v = vec![1,2,3,4,5];
// why this line doesn't work?
v.iter().map(|it| format_args!("x{}", it)).intersperse(format_args!(", ")).for_each(|args| println!("{}", args));
// whereas this line work
println!("{}", v.iter().format_with(", ", |it, f| f(&format_args!("x{}", it))));
}
The error message reads "returns a value referencing data owned by the current function", it is not owned by the current function, it is owned by the vector. What's wrong with the code?
The macro evaluates each expression argument into macro-generated locals, then passes references to those in the Arguments structure. It seems simple in your case that it could just use your given reference, but that doesn't generalize.
You can create internal type which implements std::fmt::Display, and return it from a function using impl std::fmt::Display + 'a.
With this way, You can return to-be-formatted objects without rendering them too earlily.
However, this requires lots of boilerplates and would not be useful for some cases.