Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rust and print

A little overview of the built-in print-like macros:

  • print! to STDOUT.
  • println! to STDOUT with a trailing newline.
  • eprint! to STDERR.
  • eprintln! to STDERR with a trailing newline.
  • dbg! to STDERR with bells and whistles.
  • format! returns formatted string. This is behind the printing macros.
#[allow(clippy::print_with_newline)]

fn main() {
    let fname = "Foo";
    let lname = "Bar";

    println!("{} {}", fname, lname);
    println!("{fname} {lname}");
    print!("{fname} {lname}\n");

    eprintln!("error in {} {}", fname, lname);
    eprint!("error in {} {}\n", fname, lname);

    let full_name = format!("{fname} {lname}");
    println!("{full_name}");
    dbg!(full_name);
}

Output:

Foo Bar
Foo Bar
Foo Bar
error in Foo Bar
error in Foo Bar
Foo Bar
[examples/intro/print.rs:14] full_name = "Foo Bar"

  • print!
  • println!
  • eprint!
  • eprintln!
  • dbg!
  • format!