Rust write! and writeln! - format into strings, files, and buffers
Use write! or writeln! when you want Rust formatting to go directly into a destination you already have, such as a String, file, socket, or byte buffer. writeln! adds a newline; write! does not.
use std::fmt::Write;
let mut output = String::new();
write!(&mut output, "value={}", 42).unwrap();
writeln!(&mut output, ", hex={:#x}", 42).unwrap();
assert_eq!(output, "value=42, hex=0x2a\n");Write formatted text into an existing destination
Section titled “Write formatted text into an existing destination”The macros work with values that provide write_fmt, typically through one of two traits:
std::fmt::Writefor Unicode text destinations such asStringstd::io::Writefor byte-oriented destinations such as files, sockets, and byte buffers
For a String, bring std::fmt::Write into scope. For an I/O writer, bring std::io::Write into scope.
write! vs writeln!
Section titled “write! vs writeln!”write! writes only the formatted content. writeln! also appends a newline.
Both return the result produced by the destination’s write_fmt method. That is commonly fmt::Result for formatting destinations or io::Result for I/O destinations, so application code can propagate a real write failure instead of panicking through print!.
Avoid the intermediate allocation
Section titled “Avoid the intermediate allocation”Writing directly into an existing destination avoids first building a separate String. By contrast, format! creates an owned string and the program still has to write that string somewhere afterward.
format! is useful when you actually need ownership of the finished string; write! is cleaner when the destination already exists.
For the allocation boundary, see format! and format_args!.