← 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.

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
fn main() {
    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
fn main() {
    let name = "Ferris";
    let age = 5;
    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
fn main() {
    let city = "Berlin";
    let temp = 21;
    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
fn main() {
    print!("No newline here... ");
    println!("but this one ends the line");
}
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.