macro

format_args! — Prepare formatting arguments without building a String

Create std::fmt::Arguments for immediate consumption by a formatting API. The macro itself does not allocate an owned formatted String. Its result can borrow arguments and temporaries; pass it directly to the consumer rather than assuming it can outlive them. The consumer may allocate. Expression result: std::fmt::Arguments. Expected result (not executed): stdout: 'Completed: 8\n'; no stderr. Common mistake: Returning formatting arguments that borrow local data; return an owned String if the result must outlive the inputs.

Formal Syntax & Structure

rust syntaxrust
format_args!("format string", arguments...)

Working Example

Example Snippetrust
fn show(args: std::fmt::Arguments<'_>) {    println!("{args}");}fn main() {    let completed = 8;    show(format_args!("Completed: {completed}"));}
Rust Reference