Rust Output

Rust Output – Complete Beginner Guide
If you’re learning Rust, one of the first things you’ll want to do is display output on the screen.
Printing text, variables, formatted values, debugging information — all of this is handled using Rust’s powerful output system.
In this fully beginner guide, you’ll learn:
How Rust output works
println!vsprint!Formatting text and variables
Debug printing
Pretty printing
Printing errors to stderr
Formatting specifiers
Common beginner mistakes
Best practices
Let’s dive in
Basic Output in Rust
The most common way to print output in Rust is using:
1 | println!("Hello, Rust!"); |
Explanation:
println!is a macro!indicates a macro (not a normal function)Automatically adds a newline
Output:
print! vs println!
Rust provides two basic output macros:
println!
Adds a newline at the end.
1 2 | println!("Hello"); println!("World"); |
Output:
World
print!
Does NOT add newline.
1 2 | print!("Hello "); print!("World"); |
Output:
Use println! in most cases.
Printing Variables
You can print variables using {} placeholders.
1 2 | let name = "Rust"; println!("Welcome to {}", name); |
Output:
Multiple Variables
1 2 3 4 | let language = "Rust"; let year = 2025; println!("{} was released in {}", language, year); |
Order matters.
Formatting Basics
Rust uses format string syntax similar to other languages.
1 2 | let age = 25; println!("Age: {}", age); |
Positional Arguments
1 | println!("{1} {0}", "Rust", "Hello"); |
Output:
Named Arguments
1 | println!("{language} is fast", language="Rust"); |
Cleaner and readable.
Debug Output with {:?}
To print complex data types:
1 2 | let numbers = vec![1, 2, 3]; println!("{:?}", numbers); |
Output:
Pretty Debug Printing
1 | println!("{:#?}", numbers); |
Output:
1,
2,
3,
]
Very useful for debugging.
Printing Structs
To print a struct:
1 2 3 4 5 6 7 8 9 10 11 12 | #[derive(Debug)] struct User { name: String, age: u32, } let user = User { name: String::from("Alice"), age: 30, }; println!("{:?}", user); |
Without #[derive(Debug)], printing fails.
Visual Overview of Rust Output Formatting
Printing to Standard Error (stderr)
Rust allows printing errors separately:
1 | eprintln!("This is an error message"); |
Useful for:
Logging errors
CLI applications
Formatting Numbers
Fixed decimal places:
1 2 | let pi = 3.141592; println!("{:.2}", pi); |
Output:
Padding numbers:
1 | println!("{:5}", 42); |
Adds spacing.
Zero padding:
1 | println!("{:05}", 42); |
Output:
Alignment Formatting
1 2 3 | println!("{:<10}", "Rust"); // Left align println!("{:>10}", "Rust"); // Right align println!("{:^10}", "Rust"); // Center |
Useful for tables.
Binary, Hex, and Octal Output
1 2 3 4 | println!("{:b}", 10); // Binary println!("{:x}", 255); // Hex lowercase println!("{:X}", 255); // Hex uppercase println!("{:o}", 10); // Octal |
Very useful in systems programming.
Using format! Macro
format! creates a string instead of printing.
1 2 | let message = format!("Hello {}", "Rust"); println!("{}", message); |
Used for:
Building strings
Logging
Reusable messages
Output in Loops
1 2 3 | for i in 1..4 { println!("Number: {}", i); } |
Simple and clean.
Printing Boolean Values
1 2 | let is_active = true; println!("{}", is_active); |
Output:
Printing Characters
1 2 | let letter = 'R'; println!("{}", letter); |
Common Beginner Mistakes
❌ Forgetting {} placeholder
❌ Using {:?} without deriving Debug
❌ Mixing positional arguments incorrectly
❌ Forgetting semicolon
❌ Confusing print! and println!
Why Rust Output Uses Macros
Rust uses macros because:
Supports variable arguments
Compile-time formatting checks
Type safety
Efficient code generation
This ensures formatting errors are caught at compile time.
Best Practices for Rust Output
- Use
println!for most printing - Use
{:?}for debugging - Use
format!when creating strings - Use named arguments for readability
- Use
eprintln!for errors
Output in Real CLI Applications
In real-world Rust programs:
println!→ normal outputeprintln!→ error outputLogs often handled via
logcrate
Understanding output formatting is important for CLI tools.
Frequently Asked Questions (FAQs)
1. What is println! in Rust?
println! is a macro used to print formatted text to the console with a newline.
2. What is the difference between print! and println!?
println! adds a newline. print! does not.
3. How do I print variables in Rust?
Use {} inside the string and pass variable as argument.
4. What does {:?} mean in Rust?
It prints data using Debug formatting.
5. How do I print errors in Rust?
Use eprintln! to print to standard error.
Conclusion
Rust output system is:
Powerful
Type-safe
Flexible
Efficient
You learned:
Basic printing
Formatting
Debug output
Numeric formatting
Error printing
Mastering Rust output is essential for writing CLI tools, debugging programs, and building real-world applications.
