← Back to Rust Course | Chapter 1: Setup & Basics | Lesson 7 of 7

The println! Macro

println! is the command you use to make your Rust program show text to you on the screen.
Syntax
rust
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

markup
// 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");
}

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.

Note: Ferris is the unofficial mascot of Rust -- a friendly crab.

Example: Placeholders With {}

markup
// 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);
}

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

markup
// 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);
}

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!

markup
// 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");
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using println!("{}", x, y) with more placeholders or arguments than actually match, causing a compile error.
  2. Forgetting curly braces {} are required as placeholders and cannot be replaced with old-style %s/%d formatting.
  3. Confusing print! (no trailing newline) with println! (adds a trailing newline) and getting squished-together output.
Chapter Summary
  • 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 formatted String without printing it, using the same syntax as println!.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.