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! and print!
Section titled “println! and print!”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!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.
Placeholders: {} and named variables
Section titled “Placeholders: {} and named variables”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 oldFerris is 10 years oldBoth 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!Debug printing with {:?}
Section titled “Debug printing with {:?}”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)For a nicer, multi-line layout on bigger values, use {:#?} (pretty-print):
fn main() { let point = (3, 7); println!("{:#?}", point);}Output:
( 3, 7,)Number formatting
Section titled “Number formatting”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 4200003.14You don’t need to memorize these — just know they exist for when you need to line up numbers or round decimals.
Lining up text, and printing braces
Section titled “Lining up text, and printing braces”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 PriceCoffee 3.50Bagel 2.25Total 5.75Reading {:>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}Practice
Section titled “Practice”1. A price tag
Section titled “1. A price tag”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.502. Build a label, then print it two ways
Section titled “2. Build a label, then print it two ways”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"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 canDisplaythemselves.format!works likeprintln!but returns aStringinstead 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
Section titled “Quick check”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
Read more
Section titled “Read more”std::fmt— standard library docs: the full list of formatting options, including width, fill, alignment and precision.- Formatted print — Rust by Example: runnable examples of positional and named arguments, padding and alignment.
- Debug — Rust by Example: how
{:?}and{:#?}work and how your own types get them. println!— standard library docs: the official reference forprintln!, with links toprint!andeprintln!.format!— standard library docs: building aStringwith the same formatting rules.