A Guide to Optimizing Print Output in Your Rust Programming

In Rust Program, We can use the print! macro to print strings, numbers and variables the output on the screen. For example:-

fn main() {
    print!("Hello, World!");
}

Output :

Hello, World!

Above, print! is a macro which prints the text inside double quotes.

In Rust Program, there are two alternatives of the print:

  1. print!()
  2. println!()

Print! Macro In Rust :

As above-mentioned earlier, the print! macro prints the text under double quotes. For example-

fn main() {
    print!("Rust is fun! ");
    print!("I like Rust programming.");
}

Output :

Rust is fun! I like Rust programming.

We can see that have used two print! macros to print two several strings. However, both the strings are printed in the equivalent line.

To individual the print strings in several lines, we can use the println! macro who will add a new metrical line character at the end.

println! Macro In Rust :

fn main() {
    println!("Rust is fun!");
    println!("I like Rust programming.");
}

Output :

Rust is fun!
I like Rust programming.

Above, we can see output is printed in two individual lines.

This is println! adds a recent line character (enter) at the end, that way the second text is printed in the after line.

format! Macro In Rust :

The format! macro makes a formatted string by combining verbal text with formatted expressions. It doesn’t print anything to the pacify; it returns a String that we can print or use as needed.

Basic Usage:

fn main() {
    let name = "Alice";
    let age = 30;
    
    // Create a formatted string
    let formatted_string = format!("Name: {}, Age: {}", name, age);

    // Print or use the formatted string
    println!("{}", formatted_string);
}

Options of Formatting :

The format string may comprise placeholders for evolution. This placeholders start with {} and include optional formatting parameters:

fn main() {
    let pi = 3.14159;

    // Formatting with precision
    let formatted_float = format!("Pi with precision: {:.2}", pi);

    println!("{}", formatted_float);
}

We can also use both format! and println! support various formatting options for different types. For example:

fn main() {
    let number = 42;

    // Formatting as hexadecimal
    println!("Number in hexadecimal: {:x}", number);
}

this is just basic examples, and you can discovery more detailed information in the official Rust documentation on formatting.

Print Variables In Rust :

We can conduct the same print! and println! macros to print variables in Rust Program. For example-

fn main() {
    let age = 21;
  
    // print the variable using println!
    println!("{}", age);

    // print the variable using print!
    print!("{}", age);
}

Output :

21
21

In the upon example advertising the print statements:

print!("{}", age);
println!("{}", age);

See, {} is a placeholder replaced by the value of the variable below the comma. So, we get 21 as output instead of {}.

You, can further add text with the placeholder to format your output. For example-

fn main() {
    let age = 21;
  
    // print the variable using println!
    println!("Age = {}", age);
}

Output :

Age = 21

So, you can see the output glory more informatory.

Print Multiple Variables In Rust :

We can exercise a single println! macro to print multiple variables to-gether. For example-

fn main() {
    let age = 21;
    let name = "Azad";
  
    // print the variables using println!
    println!("Name = {}, Age = {}", name, age);
}

Output :

Name = Azad, Age = 21

Above, we can see variables are printed in sequence. the first variable name make change the first placeholder and the second variable age make change the second placeholder.

Still, we can also mark the numbering for placeholders to print variables in other order. For example-

fn main() {
    let age = 21;
    let name = "Azad";
  
    // print the variable using println!
    println!("Name = {0}, Age = {1}", name, age);
}

Output :

Name = Azad, Age = 21

Below, the placeholder:-

  1. {0} is changed by the first variable name
  2. {1} is changed by the second variable age

Likewise, you can also use the variable names straight away inside the placeholder. For example-

fn main() {
    let age = 21;
    let name = "Azad";
  
    // print the variables using println!
    println!("Name = {name}, Age = {age}");
}

Output :

Name = Azad, Age = 21

Below, instead of using variables individually after comma, you have directly shifted them inside the placeholder.

  1. {name} - prints value of the name variable
  2. {age} - prints value of the age variable

Print Newline Character In Rust :

In Rust program, we can print a newline character using the println! macro or the print! macro towards with the "\n" escape sequence. Here’s an example:

fn main() {
    // Using println! macro to print with a newline
    println!("Hello, world!");

    // Using print! macro with the "\n" escape sequence
    print!("This is a line without a newline");
    print!("\n"); // Adding a newline separately

    // Another way using println! with an empty format string
    println!();

    // You can also use the write! macro from the std::fmt module
    use std::fmt::Write;
    let mut buffer = String::new();
    write!(&mut buffer, "This is written to a string without a newline").unwrap();
    writeln!(&mut buffer).unwrap(); // Adding a newline separately
    println!("{}", buffer);
}

Output :

Hello, world!
This is a line without a newline
This is written to a string without a newline
Previous
A Comprehensive Guide to Effective Comments In Rust Programming
Next
A Guide To Variables and Mutability In Rust Programming