Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Rust Dictionary

  1. Home
  2. Rust Dictionary
  3. println!() / print!() / format!() / eprintln!()

println!() / print!() / format!() / eprintln!()

Rust provides the macros println!(), print!(), format!(), and eprintln!() for output and string formatting. These macros support flexible output using format specifiers in a format string.

Syntax

// Prints to standard output with a newline.
println!("text");
println!("{}", value);
println!("{:?}", value);      // Debug format
println!("{:#?}", value);     // Pretty Debug format

// Prints to standard output without a newline.
print!("{}", value);

// Creates a formatted String (does not print).
let s = format!("x = {}", x);

// Prints to standard error with a newline.
eprintln!("Error: {}", msg);
eprint!("{}", msg);           // Prints to standard error without a newline

// Common format specifiers
println!("{:5}", x);          // Width 5 (right-aligned)
println!("{:<5}", x);         // Width 5 (left-aligned)
println!("{:05}", x);         // Width 5, zero-padded
println!("{:.2}", f);         // 2 decimal places
println!("{:8.2}", f);        // Width 8, 2 decimal places
println!("{:b}", x);          // Binary
println!("{:o}", x);          // Octal
println!("{:x}", x);          // Hexadecimal (lowercase)
println!("{:X}", x);          // Hexadecimal (uppercase)
println!("{:e}", f);          // Exponential notation (lowercase)

Macro List

MacroDescription
println!()Prints to standard output with a newline.
print!()Prints to standard output without a newline.
format!()Creates a formatted String without printing it.
eprintln!()Prints to standard error (stderr) with a newline.
eprint!()Prints to standard error without a newline.
{}Formats a value using the Display trait.
{:?}Formats a value using the Debug trait.
{:#?}Formats a value using the Debug trait with pretty-printing.
{:b} {:o} {:x}Formats as binary, octal, and hexadecimal, respectively.
{:.2}Specifies the number of decimal places.

Sample Code

fn main() {
    // Basic output
    println!("Hello, Rust!");
    println!("x = {}, y = {}", 42, 3.14);

    // Named arguments
    println!("{name} is {age} years old", name = "Alice", age = 30);

    // Variable capture (Rust 1.58 and later)
    let x = 100;
    println!("x = {x}");

    // Debug / Pretty Debug format
    let v = vec![1, 2, 3];
    println!("{:?}", v);    // [1, 2, 3]
    println!("{:#?}", v);   // Multi-line pretty-printed output

    // Numeric format specifiers
    println!("{:8}", 42);       // "      42" (width 8, right-aligned)
    println!("{:<8}", 42);      // "42      " (width 8, left-aligned)
    println!("{:^8}", 42);      // "   42   " (width 8, center-aligned)
    println!("{:08}", 42);      // "00000042" (width 8, zero-padded)
    println!("{:.3}", 3.14159); // "3.142" (3 decimal places)
    println!("{:8.2}", 3.14);   // "    3.14" (width 8, 2 decimal places)

    // Radix formatting
    let n = 255u32;
    println!("Decimal: {n}");       // 255
    println!("Binary: {:b}", n);    // 11111111
    println!("Octal: {:o}", n);     // 377
    println!("Hex lowercase: {:x}", n); // ff
    println!("Hex uppercase: {:X}", n); // FF
    println!("With prefix: {:#x}", n);  // 0xff

    // Use format!() to create a String.
    let s = format!("{:>10}", "rust");
    println!("Right-aligned: '{s}'");  // '      rust'

    // Use eprintln!() for error output (writes to stderr).
    eprintln!("Warning: this is error output");

    // print!() and flush (flushing may be needed after output without a newline)
    use std::io::Write;
    print!("Enter input: ");
    std::io::stdout().flush().unwrap();
}

Notes

Rust's output macros use a format string to specify formatting. {} calls the Display trait, and {:?} calls the Debug trait. To use {:?} with your own types, simply add #[derive(Debug)].

The format!() macro does not print anything — it returns a String. It is useful for building log messages or converting multiple values into a string.

If you want output to appear immediately after using print!() without a newline, you need to call std::io::stdout().flush() because output is buffered. For information on implementing the Display and Debug traits, see Display Trait / Debug Trait.

If you find any errors or copyright issues, please .