Skip to content
Related Articles

Related Articles

Rust – Formatting Print Statements

View Discussion
Improve Article
Save Article
  • Last Updated : 25 Jul, 2022
View Discussion
Improve Article
Save Article

In Rust, we use macros to print formatted texts in the editor. Macros provide functionality similar to functions but without the runtime cost. Print statements are used extensively in programming. They are used for prompting users for information or for debugging the program. Macros in Rust end with an exclamatory mark (!). 

Some of the Macros used are:

  • format!:  Converts formatted text to String
  • print!: print! is similar to format! but the text gets printed to the console
  • println!: println! is similar to print! but a newline gets appended.
  • eprint!: eprint! is similar to print! with the output sent to the standard error being the exception.
  • eprintln!: eprintln! is similar to eprint!  except it appends a new line at the end of the string.

Here, we discuss the most commonly used macros:

1. format!: 

format! is mainly used in the concatenation of strings. The first argument format! receives is a format string which should be a string literal.

Example 1: 

Rust




// Rust program for format!
fn main() {
    // it formats the enclosed string
    // in the braces till the 5th index
    let new_string= format!("Geeks for Geeks {:.6}", "Courses");
    println!("{}", new_string);
}

Output:

 

2. print!:  

print! is a macro that prints formatted text to the console (or stdout). print! receives multiple arguments where the first argument is the literal string and the curly braces {} in the first argument are replaced by the referenced values which result in the formatting of the string being printed to the console.

In the example below, the curly braces will is replaced by Courses and the resultant formatted string is sent to the console(Output).

Example 2:

Rust




// Rust program for ptint!
fn main() {
    // \n -> new line character
    print!("Geeks for Geeks {}!\n", "Courses");
}

Output:

 

Example 3:

Rust




// The first curly braces{} is replaced
// with GeeksforGeeks, the second  braces {} 
// is replaced with Jonathon, and the third
// {} is replaced with 2.5 
fn main() {
    print!("The {} {} is generally of {} hours ",
      "GeeksforGeeks", "Jobathon",2.5);
}

Output:

 

Example 4:

Rust




// Rust print example
fn main() {
    print!("The coordinates of Noida: {x}N,{y}E",
      x=28.55,y=77.39);
}

Output:

 

3. println!: 

The println! macro is the same as a print! except that it automatically adds the new line \n automatically at the end of the string.

Example 5:

Rust




// Rust PrintIn program
fn main() {
    let a = 10.0;
    let b = 3.0;
    let c=a/b;
    // prints decimal upto 3 decimal places
    println!("c is: {:.3}",c);
      
    // appends three blank spaces at first and then
    // 3 digits after the decimal and the total 
    // no: of digits is 8
    println!("c is: {:8.3}",c);
      
    // appends three zeros at first and then 3 digits after 
    // decimal and the total no: of digits is 8
    println!("c is: {:08.3}",c);
}

Output:

 

4. eprint!: 

The eprint! is similar to print!  except that it displays the output as an error and is used for reporting error messages. The following example prints “GFG” and “Course” on the same line but as an error.

Example 6: 

Rust




// Rust program for eprint!
fn main() {
    eprint!("GFG");
    eprint!(" Courses");
}

Output:

 

5. eprintln!

The eprint!() macro displays the output as an error and appends a new line(\n) at the end of it. The following example prints “GFG” as an error and appends a new line to it, then prints “Course” and appends a new line.

Example 7: 

Rust




// Rust program for eprintln!
fn main() {
  eprintln!("GFG");
  eprintln!("Course");
}

Output:

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!