Rust — Output
println! — your main window into programs
fn main() {
println!("Hello, world!");
}
The ! marks println as a macro, which is why it can accept unlimited arguments of many different types — a normal function couldn't.
Placeholder formatting
Empty braces consume arguments in order:
let name = "Ada";
let age = 36;
println!("{} is {} years old", name, age);
// Ada is 36 years old
Numbered placeholders reuse or reorder:
println!("{0} meets {0}'s friend {1}", "Ada", "Bo");
// Ada meets Ada's friend Bo
Named arguments read best:
println!("{name} scored {score}", name = "Ada", score = 95);
And variables in scope can be referenced directly by name — the most modern style:
let city = "Ahmedabad";
println!("Living in {city}");
// Living in Ahmedabad
Format specifiers — after the colon
let pi = 3.14159;
println!("{:.2}", pi); // 3.14 — two decimals
println!("{:>8}|", "hi"); // hi| right-align width 8
println!("{:<8}|", "hi"); // hi | left-align
println!("{:^8}|", "hi"); // hi | center
println!("{:05}", 42); // 00042 — zero-padded
println!("{:+}", 42); // +42 — force sign
println!("{:b} {:x} {:o}", 5, 255, 8); // 101 ff 10
Combining: {name:>10.2} = right-aligned, width 10, two decimals.
Debug printing — {:?}
Complex types refuse plain {} unless you implement Display. The debug marker prints anything that derives Debug:
let numbers = vec![1, 2, 3];
// println!("{}", numbers); ❌ compile error
println!("{:?}", numbers); // [1, 2, 3] ✓
println!("{:#?}", numbers); // pretty-printed multi-line
Deriving it on your own structs:
#[derive(Debug)]
struct Point { x: i32, y: i32 }
let p = Point { x: 3, y: 4 };
println!("{:?}", p); // Point { x: 3, y: 4 }
eprintln! — errors go to stderr
eprintln!("Something failed: {}", reason);
Standard output (println) is for program results; standard error (eprintln) for diagnostics — so shell pipes like myapp > out.txt stay clean.
Escaping braces & common sequences
println!("{{}}"); // prints {} — double to escape
println!("line1\nline2"); // \n newline
println!("col1\tcol2"); // \t tab
println!("quote \" inside");
println!("backslash \\");
print! vs println!
print!("no newline here → ");
print!("same line\n");
print! omits the trailing newline; output may sit in a buffer until the program ends — another reason println! dominates.
Mini Practice
- Print three values with positional
{0} {1} {0}reordering. - Render a mini price table using
{:>8.2}alignment. - Derive Debug on a struct and pretty-print with
{:#?}. - Escape literal braces around a value: show
{value}as text. - Send one line via println! and one via eprintln!, then pipe stdout to a file.
Next: comments →
Related Topics
Frequently Asked Questions about Output
What is Output in Rust?
Output is a fundamental concept in Rust. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Output?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Output.
Why is Output important in Rust?
Output is essential for Rust development. Understanding this concept will help you write better code and solve real-world problems more effectively.