performanceallocationformatting

format! in a hot loop

Allocating a fresh String per iteration to build messages that get discarded or appended.

Why this looks tempting

format! is the obvious tool for interpolation, and the loop body reads cleanly.

Why wrong

Every format! call is a new heap allocation plus the formatter's per-call setup. In a tight loop that's allocation traffic dominating the workload — and if the result is just appended to a buffer, you've allocated twice for every line.

Fix

Reuse a single String buffer with write!(&mut buf, "...", ...) and buf.clear() between iterations. For final concatenation reach for write! against the target writer directly. Reserve format! for one-shot or cold-path messages.

Canonical alternative

performance/memory-optimization