Rust Syntax & Basic Constructs
Estimated reading: 3 minutes 35 views

🦀 Rust – Output: Displaying Data Using print! and println!

🧲 Introduction – Why Learn Output Formatting in Rust?

In any programming language, printing values is essential for debugging, testing, and user interaction. Rust uses the powerful macros print! and println! for output, offering clean formatting, placeholders, and compile-time safety.

🎯 In this guide, you’ll learn:

  • The difference between print! and println!
  • How to format text and variables in output
  • Display multiple values with placeholders
  • Best practices and common mistakes to avoid

🖨️ Basic Rust Output Syntax

fn main() {
    println!("Hello, Rust!");
}

📤 Output:

Hello, Rust!
MacroDescription
print!Outputs text without a newline
println!Outputs text with a newline (\n) added

🧾 Printing Strings and Variables

fn main() {
    let lang = "Rust";
    let version = 2025;

    println!("Welcome to {} v{}", lang, version);
}

📤 Output:

Welcome to Rust v2025

🔍 Explanation:

  • {} ➜ Placeholder for a value
  • Values are inserted in order as per their position

📦 Formatting Numbers and Expressions

fn main() {
    println!("2 + 3 = {}", 2 + 3);
    println!("Binary of 10: {:b}", 10);
    println!("Hex of 255: {:x}", 255);
    println!("Padded number: {:05}", 42);
}

📤 Output:

2 + 3 = 5
Binary of 10: 1010
Hex of 255: ff
Padded number: 00042
Format CodeMeaning
{:b}Binary
{:x}Hexadecimal (lowercase)
{:X}Hexadecimal (uppercase)
{:o}Octal
{:05}Pad with 0s to width 5

🔄 Output Without Newline Using print!

fn main() {
    print!("Loading");
    print!("...");
}

📤 Output:

Loading...

⚠️ Use print! when you need to stay on the same line (like CLI progress indicators).


📚 Named Placeholders (Optional Enhancement)

fn main() {
    let name = "Rust";
    let year = 2025;

    println!("Welcome to {language} in {year}!", language = name, year = year);
}

📤 Output:

Welcome to Rust in 2025!

🚫 Common Output Mistakes

MistakeError or BehaviorSolution
Missing ! in printlnError: cannot find function printlnUse println! (macro, not a fn)
Wrong number of argumentsRuntime panic or compile errorMatch {} with correct # of values
Mixing print! and println! poorlyFormatting bugs (e.g., no newlines)Be explicit about output layout

📌 Summary – Recap & Next Steps

Mastering output formatting in Rust helps you inspect program behavior and create user-friendly applications. From simple println! usage to formatted numbers and named arguments, Rust gives you precise control over how data is displayed.

🔍 Key Takeaways:

  • Use println! for output with newline; print! without newline
  • {} is used for inserting variables into output
  • Supports format codes like :x, :b, :05
  • Output macros are safe, fast, and easy to use

⚙️ Ready to move on? Learn how to use comments to document and organize your Rust code next.


❓FAQs


Why is there a ! in println!?
println! is a macro, not a regular function. Macros in Rust are invoked using ! and expand at compile time.


Can I print multiple values in one statement?
✅ Yes. Use multiple {} placeholders and pass values in order, or use named arguments.


What’s the difference between print! and println!?
println! adds a newline (\n) at the end. print! doesn’t—great for progress bars or inline messages.


How do I print a variable in Rust?
✅ Use {} inside the macro and pass the variable as an argument:

let age = 25;
println!("Age: {}", age);

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Rust – Output

Or Copy Link

CONTENTS
Scroll to Top