Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Macros

Rust println! and print! macros

Reference for the Rust println! and print! macros: format strings, positional and named arguments, inline captures, stdout buffering and when they panic.

println! formats its arguments according to a format string and writes the result to standard output followed by a newline. print! does the same without the newline. They are the first macros every Rust program uses, and their format language is shared with format!, write!, panic! and the logging macros.

The format string is checked at compile time: the number and kind of arguments must match the placeholders, and a type must implement the trait the placeholder asks for (Display for {}, Debug for {:?}).

Rust
let name = "Ada";
println!("Hello, {name}! 2 + 2 = {}", 2 + 2);

Try it

Rust Placeholders and arguments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust print! and flushing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
println!();
println!("format string", args...);
print!("format string", args...);
Name Description
format string A string literal (it must be a literal, not a variable) with {} placeholders and {{, }} escapes.
args Expressions, positional or name = expr. Variables in scope can be named directly inside the braces.

Returns: (). The macros expand to a call on the process-wide stdout handle.

Errors / panics / edge cases: they panic if writing to stdout fails (for example, with a full disk when stdout is redirected to a file). A closed stdout is silently ignored. Mismatched placeholders and arguments are compile errors. Only simple names can be captured inline: {self.x} or {v[0]} is not allowed, so write {} with an argument.

Usage notes

  • {} needs Display, {:?} needs Debug. Vectors, options and tuples implement only Debug; printing a Vec with {} is a compile error.
  • Format spec after the colon: width, fill, alignment, sign, precision, #, radix. See Format specifiers.
  • Line buffering: stdout is line-buffered. print! text without a newline may not appear until you flush with io::stdout().flush() or print a newline. On exit, the buffer is flushed.
  • Locking: each call locks stdout. In a hot loop, take stdout().lock() once and use writeln!, or wrap it in BufWriter for large outputs.
  • Threads: output from one macro call is not interleaved with another thread's call because of the lock, but two separate calls may interleave.
  • Not for errors: diagnostics belong on stderr; use eprintln!.
  • Testing: cargo test captures output from println! and shows it only for failing tests, unless you pass -- --nocapture.
  • Edition note: inline captures like work since Rust 1.58 in every edition.
  • Escape: write {{ and }} for literal braces. A lone { is a compile error.