Skip to content

Rust Output

You’ve already used println! a bunch of times. This page covers the other printing macros you’ll run into and when to reach for each one.

println! prints text and adds a newline at the end. print! does the same thing but doesn’t add a newline:

fn main() {
print!("Hello, ");
print!("World");
println!("!");
}

Output:

Hello, World!
▶ Try it Yourself

Notice all three calls printed on the same line — only println! breaks to a new one. In practice you’ll use println! almost all the time; print! is mainly useful when you’re building up a line piece by piece.

You’ve seen {} used as a placeholder. You can fill it either by passing a value after the string, or by putting the variable name directly inside the braces:

fn main() {
let name = "Ferris";
let age = 10;
println!("{} is {} years old", name, age); // positional
println!("{name} is {age} years old"); // inline (same result)
}

Output:

Ferris is 10 years old
Ferris is 10 years old
▶ Try it Yourself

Both lines print the same thing. The inline style ({name}) is a bit newer and usually easier to read, so prefer it when you can.

format! — build a string instead of printing it

Section titled “format! — build a string instead of printing it”

Sometimes you want the formatted text as a String instead of printing it immediately — say, to store it or pass it to another function. That’s what format! is for. It works exactly like println! but returns a String instead of printing:

fn main() {
let name = "Ferris";
let message = format!("Hello, {name}!");
println!("{message}");
}

Output:

Hello, Ferris!
▶ Try it Yourself

Plain {} only works for values that know how to display themselves as text — numbers, strings, booleans. For more complex values like tuples or structs, {} won’t compile. Use {:?} (the “debug” format) instead:

fn main() {
let point = (3, 7);
println!("{:?}", point);
}

Output:

(3, 7)
▶ Try it Yourself

For a nicer, multi-line layout on bigger values, use {:#?} (pretty-print):

fn main() {
let point = (3, 7);
println!("{:#?}", point);
}

Output:

(
3,
7,
)
▶ Try it Yourself

A few formatting tricks that come up often:

fn main() {
let pi = 3.14159;
println!("{:.2}", pi); // 2 decimal places -> 3.14
println!("{:5}", 42); // pad to width 5 -> " 42"
println!("{:08.2}", pi); // pad with zeros -> 00003.14
}

Output:

3.14
42
00003.14
▶ Try it Yourself

You don’t need to memorize these — just know they exist for when you need to line up numbers or round decimals.

Width works for text too, and you can choose which side the value sticks to. After the : put < (left), > (right) or ^ (center), then the width. You can also put a fill character in front, like -:

fn main() {
let coffee = 3.5;
let bagel = 2.25;
println!("{:-^20}", " MENU ");
println!("{:<12}{:>8}", "Item", "Price");
println!("{:<12}{:>8.2}", "Coffee", coffee);
println!("{:<12}{:>8.2}", "Bagel", bagel);
println!("{:<12}{:>8.2}", "Total", coffee + bagel);
}

Output:

------- MENU -------
Item Price
Coffee 3.50
Bagel 2.25
Total 5.75
▶ Try it Yourself

Reading {:>8.2} piece by piece: > means right-align, 8 is the width, and .2 means two decimal places. Right-aligning numbers makes the decimal points line up, which is why receipts and tables do it.

What if you want a real { in your output? Since braces mean “placeholder”, you double them: {{ prints { and }} prints }.

fn main() {
println!("Set: {{{}, {}}}", 1, 2);
}

Output:

Set: {1, 2}
▶ Try it Yourself

Store the item "Notebook" and the price 2.5, then print Notebook costs $2.50. Hint: {:.2} rounds to two decimal places, and it works inline too: {price:.2}.

Show solution
fn main() {
let item = "Notebook";
let price = 2.5;
println!("{item} costs ${price:.2}");
}

Output:

Notebook costs $2.50
▶ Try it Yourself

Use format! to build a label ferris-7 from the name "ferris" and the number 7. Print it once with {} and once with {:?}, and spot the difference. Hint: format! takes exactly the same placeholders as println!.

Show solution
fn main() {
let name = "ferris";
let id = 7;
let label = format!("{name}-{id}");
println!("{label}");
println!("{label:?}");
}

Output:

ferris-7
"ferris-7"
▶ Try it Yourself

Debug printing shows text with quotes, so you can see exactly where it starts and ends. That’s handy when a value has stray spaces.

  • println! prints with a newline; print! doesn’t.
  • {} and {name} are placeholders for values that can Display themselves.
  • format! works like println! but returns a String instead of printing.
  • {:?} (and {:#?}) prints values for debugging, including ones {} can’t handle.
  • {:.2} rounds decimals; <, > and ^ with a width line text up; {{ and }} print literal braces.

Quick check

1. What's the difference between println! and print!?

2. Why would you use {:?} instead of {} to print a tuple?

Score: 0 / 2