Rust Output

Printing and formatting data in Rust programs

🖨️ Rust Output Basics

Learn to display data using Rust's powerful printing macros. Master println!, print!, format!, and advanced formatting options. Rust provides type-safe string formatting with compile-time checking to prevent runtime errors in output operations.


fn main() {
    let name = "Rust";
    let version = 1.70;
    
    println!("Hello, {}!", name);
    println!("Version: {:.2}", version);
    println!("Binary: {:b}, Hex: {:x}", 42, 42);
}
                                    

Output:

Hello, Rust!

Version: 1.70

Binary: 101010, Hex: 2a

Output Methods

📝

println!

Print with newline

println!("Hello, world!");
println!("Value: {}", 42);
➡️

print!

Print without newline

print!("Loading");
print!("...");
println!("Done!");
🔧

format!

Create formatted strings

let msg = format!("Hello, {}!", name);
println!("{}", msg);
⚠️

eprintln!

Print to standard error

eprintln!("Error: {}", error_msg);

🔹 Basic Printing

The most common way to output data in Rust:

fn main() {
    // Simple text
    println!("Welcome to Rust!");
    
    // Print without newline
    print!("Loading");
    print!(".");
    print!(".");
    println!("Done!");
    
    // Empty line
    println!();
    
    // Multiple lines
    println!("Line 1");
    println!("Line 2");
    println!("Line 3");
}

Output:

Welcome to Rust!

Loading..Done!

Line 1

Line 2

Line 3

🔹 Formatting Variables

Use placeholders to insert variables into output:

fn main() {
    let name = "Alice";
    let age = 30;
    let height = 5.6;
    let is_student = false;
    
    // Basic placeholder
    println!("Name: {}", name);
    println!("Age: {}", age);
    
    // Multiple variables
    println!("Hello, {}! You are {} years old.", name, age);
    
    // Positional arguments
    println!("{0} is {1} years old. {0} likes Rust!", name, age);
    
    // Named arguments
    println!("{name} is {age} years old and {height} feet tall", 
             name=name, age=age, height=height);
    
    // Different data types
    println!("Student status: {}", is_student);
}

Output:

Name: Alice

Age: 30

Hello, Alice! You are 30 years old.

Alice is 30 years old. Alice likes Rust!

Alice is 30 years old and 5.6 feet tall

Student status: false

🔹 Advanced Formatting

Rust provides powerful formatting options:

fn main() {
    let number = 42;
    let pi = 3.14159;
    let large_number = 1234567;
    
    // Number formatting
    println!("Decimal: {}", number);
    println!("Binary: {:b}", number);
    println!("Octal: {:o}", number);
    println!("Hexadecimal: {:x}", number);
    println!("Hexadecimal (uppercase): {:X}", number);
    
    // Float precision
    println!("Pi: {}", pi);
    println!("Pi (2 decimals): {:.2}", pi);
    println!("Pi (4 decimals): {:.4}", pi);
    
    // Width and alignment
    println!("Right aligned: {:>10}", number);
    println!("Left aligned: {:<10}", number);
    println!("Center aligned: {:^10}", number);
    println!("Zero padded: {:08}", number);
    
    // Thousands separator (using external crate)
    println!("Large number: {}", large_number);
}

Output:

Decimal: 42

Binary: 101010

Octal: 52

Hexadecimal: 2a

Hexadecimal (uppercase): 2A

Pi: 3.14159

Pi (2 decimals): 3.14

Pi (4 decimals): 3.1416

Right aligned: 42

Left aligned: 42

Center aligned: 42

Zero padded: 00000042

Large number: 1234567

🔹 Debug Output

Use debug formatting for development and troubleshooting:

fn main() {
    let numbers = vec![1, 2, 3, 4, 5];
    let tuple = (42, "hello", true);
    let name = "Rust";
    
    // Debug formatting with {:?}
    println!("Vector: {:?}", numbers);
    println!("Tuple: {:?}", tuple);
    
    // Pretty debug formatting with {:#?}
    println!("Pretty vector: {:#?}", numbers);
    
    // Regular vs debug formatting
    println!("Regular: {}", name);
    println!("Debug: {:?}", name);
    
    // Multiple debug values
    println!("Data: {:?}, {:?}, {:?}", numbers, tuple, name);
}

Output:

Vector: [1, 2, 3, 4, 5]

Tuple: (42, "hello", true)

Pretty vector: [

1,

2,

3,

4,

5,

]

Regular: Rust

Debug: "Rust"

Data: [1, 2, 3, 4, 5], (42, "hello", true), "Rust"

🔹 Creating Formatted Strings

Use format! macro to create strings instead of printing:

fn main() {
    let name = "Bob";
    let score = 95;
    
    // Create formatted strings
    let greeting = format!("Hello, {}!", name);
    let result = format!("{} scored {} points", name, score);
    let detailed = format!("Player: {name}, Score: {score}/100");
    
    // Use the formatted strings
    println!("{}", greeting);
    println!("{}", result);
    println!("{}", detailed);
    
    // Store in variables for later use
    let messages = vec![
        format!("Welcome, {}", name),
        format!("Your score: {}", score),
        format!("Grade: {}", if score >= 90 { "A" } else { "B" }),
    ];
    
    for message in messages {
        println!("• {}", message);
    }
}

Output:

Hello, Bob!

Bob scored 95 points

Player: Bob, Score: 95/100

• Welcome, Bob

• Your score: 95

• Grade: A

🧠 Test Your Knowledge

Which macro prints with a newline?