RRust By Example
beginnerBasics

Formatted Print

Use format!, print!, and println! macros for formatted output.

Formatted Print

Use format!, print!, and println! macros for formatted output.

Difficulty

Beginner

Code

rust
fn main() {
    println!("{} days", 31);
    println!("{}, this is {1}. {0}, meet {1}", "Alice", "Bob");
    println!("{name} => {value}", name = "key", value = 42);
    println!("{:b} {:o} {:x}", 10, 10, 10);
    println!("{:.3}", 3.14159);
}

Explanation

Rust provides formatting macros: println!, format!, print!. Use {} for placeholders, {:?} for debug output.

Key Concepts

  • Read the code carefully and understand the data flow
  • Try modifying the example to see how it changes behavior
  • Run this code in the Rust Playground

Related Topics

Browse more examples in the basics category to build a complete understanding of this topic.

More Basics Examples