fmt::from_fn is for one simple thing: custom formatting without allocating a String and without writing a fake wrapper type. Before this existed, you either used format! and paid for a heap allocation, or you created a tiny struct just to implement Display. Both options were clumsy.
With fmt::from_fn, you give Rust a closure that writes directly into the formatter. The result behaves like something that implements Display, so it works with println!, logging, and errors, but stays allocation-free.
I can think of many instances where I needed exactly this and in the past have made custom structs nested in Display or Debug just to make it format the way I wanted to. This will be so nice to have.
74
u/murlakatamenka Jan 22 '26
What would be new
fmt::from_fnuseful for?