The println! Macro
println! is the command you use to make your Rust program show text to you on the screen.In this page:
println!("text");
println!("value: {}", expr);
println!("{name}: {:?}", debug_value);
Basic Printing
The simplest use of println! takes a plain string literal and prints it followed by a newline. This is the very first thing most people write when learning Rust.
Example: Basic Printing
// Entry point: execution of the program starts here
fn main() {
// Print "Simple text output" to the console (with a trailing newline)
println!("Simple text output");
}
Login to try C/C++/Java/PHP code in the editor
Placeholders With {}
Curly braces {} act as placeholders inside the format string, filled in order by the extra arguments passed after the string. This lets you mix literal text with dynamic values in one call.
Example: Placeholders With {}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `name`, set to `"Ferris"`
let name = "Ferris";
// Declare an immutable variable `age`, set to `5`
let age = 5;
// Print "{} is {} years old" to the console, substituting in `name, age` (with a trailing newline)
println!("{} is {} years old", name, age);
}
Login to try C/C++/Java/PHP code in the editor
Named Arguments
You can name placeholders directly inside the braces, like {value}, and Rust matches them to named arguments passed after the format string, regardless of their position. This can make longer format strings easier to read.
Example: Named Arguments
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `city`, set to `"Berlin"`
let city = "Berlin";
// Declare an immutable variable `temp`, set to `21`
let temp = 21;
// Print "It is {} degrees in {}" to the console, substituting in `temp, city` (with a trailing newline)
println!("It is {} degrees in {}", temp, city);
}
Login to try C/C++/Java/PHP code in the editor
print! vs println!
print! behaves exactly like println! except it does not add a trailing newline. This means consecutive print! calls continue on the same line, which is useful for building output piece by piece.
Example: print! vs println!
// Entry point: execution of the program starts here
fn main() {
// Print "No newline here... " to the console (without a newline)
print!("No newline here... ");
// Print "but this one ends the line" to the console (with a trailing newline)
println!("but this one ends the line");
}
Login to try C/C++/Java/PHP code in the editor
- Using
println!("{}", x, y)with more placeholders or arguments than actually match, causing a compile error. - Forgetting curly braces
{}are required as placeholders and cannot be replaced with old-style%s/%dformatting. - Confusing
print!(no trailing newline) withprintln!(adds a trailing newline) and getting squished-together output.
println!prints text followed by a newline;print!prints without one.- Curly braces
{}are placeholders filled in by the arguments that follow, in order. - Named or positional arguments can be used inside the braces, like
{name}or{0}. format!builds a formattedStringwithout printing it, using the same syntax asprintln!.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: