A macro is a code-generation construct marked with !. Unlike a function, which returns data at runtime, a macro expands into program code and helps remove repetitive boilerplate. section 1.3.2 section 1.5
Rust's printing macros demonstrate several useful ideas:
- println! writes formatted output to standard output, while eprintln! writes to standard error. section 1.5
- Their first string literal can contain a formatting mini-language: {} uses a programmer-defined representation, while {:?} requests a default representation. section 1.5
- The macro handles type detection for arbitrary values, hiding repetitive implementation work behind a compact call. section 1.3.2
Printing several greetings
A greeting function can call println!("Hello, world!") and then iterate over stored German and Japanese greetings, passing each value to println!("{}", ®ion). Running the program prints all three messages, including their Unicode characters. 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!("{}", ®ion); #6
}
}
fn main() {
greet_world(); #7
}
Inspect the complete greeting program to see macro calls embedded in an ordinary function and loop. section 1.3.2








