Rust Output

Rust Tutorial

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! vs print!

  • 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:


 

Explanation:

  • println! is a macro

  • ! indicates a macro (not a normal function)

  • Automatically adds a newline

Output:

Hello, Rust!

print! vs println!

Rust provides two basic output macros:

println!

Adds a newline at the end.


 

Output:

Hello
World

print!

Does NOT add newline.


 

Output:

Hello World

Use println! in most cases.


Printing Variables

You can print variables using {} placeholders.


 

Output:

Welcome to Rust

Multiple Variables


 

Order matters.


Formatting Basics

Rust uses format string syntax similar to other languages.


 


Positional Arguments


 

Output:

Hello Rust

Named Arguments


 

Cleaner and readable.


 Debug Output with {:?}

To print complex data types:


 

Output:

[1, 2, 3]

Pretty Debug Printing


 

Output:

[
1,
2,
3,
]

Very useful for debugging.


 Printing Structs

To print a struct:


 

Without #[derive(Debug)], printing fails.


 Visual Overview of Rust Output Formatting

Rust Output

 Printing to Standard Error (stderr)

Rust allows printing errors separately:


 

Useful for:

  • Logging errors

  • CLI applications


Formatting Numbers

Fixed decimal places:


 

Output:

3.14

Padding numbers:


 

Adds spacing.


Zero padding:


 

Output:

00042

Alignment Formatting


 

Useful for tables.


Binary, Hex, and Octal Output


 

Very useful in systems programming.


Using format! Macro

format! creates a string instead of printing.


 

Used for:

  • Building strings

  • Logging

  • Reusable messages


Output in Loops


 

Simple and clean.


 Printing Boolean Values


 

Output:

true

Printing Characters


 


 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 output

  • eprintln! → error output

  • Logs often handled via log crate

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.

You may also like...