macro

write! — Write formatted text into an existing destination

Send formatted content to a destination with a write_fmt method. Import the relevant Write trait for a String or an I/O destination. The result is the destination method's result, commonly fmt::Result or io::Result; handle failure rather than assuming every writer succeeds. Expression result: writer-dependent result. Expected result (not executed): stdout: 'Score: 8 / 10\n'; no stderr. Common mistake: Importing std::io::Write when the destination is a String, which uses std::fmt::Write.

Formal Syntax & Structure

rust syntaxrust
write!(destination, "format string", arguments...)

Working Example

Example Snippetrust
use std::fmt::Write as _;fn main() {    let mut report = String::from("Score: ");    write!(&mut report, "{} / {}", 8, 10).expect("formatting failed");    println!("{report}");}
Rust Reference