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

This example demonstrates how to use formatted print in Rust. Read the code carefully to understand the flow. Pay attention to where values are created, borrowed, moved, or consumed.

Key Concepts

  • Rust's strong type system catches errors at compile time
  • Ownership and borrowing rules ensure memory safety
  • Pattern matching makes code expressive and exhaustive

Related Topics

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

More Basics Examples