format!, println!, print!
format!(format_string, ...) -> String
println!(format_string, ...)
print!(format_string, ...)Build a String or write formatted text to standard output. These are standard-library macros, not functions, and not methods on str.
| Parameter | Description |
|---|---|
format_string | A string literal with placeholders such as {}, {name}, {:?}, or {:#?}. Ordinary braces that should appear in the output are written as {{ and }}. |
... | Values to format. Positional arguments follow the string. Named values already in scope can be captured as {name} in edition 2021 and later. |
Returns: format! returns a String. println! and print! return () after writing to standard output.
- A trailing newline comes from println!, not from format!. format!("port {port}") does not add a newline of its own.
- These macros format through the Display or Debug traits. They do not serialize data as JSON; that is not part of std.
fn main() {
let port = 80;
let text = format!("listen on {port}");
println!("{text}");
print!("port={port}");
println!(" ready");
}Expected output
listen on 80
port=80 readyRelated: format! macro, println! macro