liveBook · Manning

Println! macro

As explained in Rust in Action: Systems programming concepts and techniques

println! is Rust's standard macro for printing formatted output followed by a newline.

println! is Rust’s standard macro for printing formatted output followed by a newline. The exclamation mark distinguishes it from an ordinary function call. Macros can reduce boilerplate and perform type detection so that different kinds of values can be printed. section 1.3.2 section 1.5

Use println! to:

  • Print a fixed string, such as println!("Hello, world!"). section 1.3.2
  • Insert a value with the {} placeholder, which uses a programmer-defined string representation. section 1.5
  • Print values such as strings and other data types to standard output, or stdout. section 1.3.2 section 1.5

Printing several languages

fn greet_world() { println!("Hello, world!"); let regions = ["Grüß Gott!", "ハロー・ワールド"]; for region in regions.iter() { println!("{}", &region); } } This prints three lines, including UTF-8 strings in German and Japanese. section 1.3.2

Listing 1.1 “Hello World!” in three languages
 fn greet_world() {
     println!("Hello, world!");                   #1
     let southern_germany = "Grüß Gott!";         #2
     let japan = "ハロー・ワールド";                    #3
     let regions = [southern_germany, japan];     #4
     for region in regions.iter() {               #5
             println!("{}", &region);             #6
     }
 }
 
 fn main() {
     greet_world();                               #7
 }

Inspect a complete greeting program showing fixed strings, a formatted placeholder, iteration, and the resulting output. section 1.3.2

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Explains macro notation, stdout, placeholders, and type handling, but treats macros as function-like rather than exposing expansion internals.
Examples
Demonstrates greetings in three languages, iterated strings, formatted placeholders, and CSV-processing output.
What you can do after
Apply it via Rust syntax to print messages and formatted values during small programs and data-processing tasks.
How it connects
Connects macros with functions, stdout, stderr, string literals, formatting, and eprintln!.

Also explained in

Compare how 2 books explain println! macro →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.