Link Search Menu Expand Document

Printing Styles

The table below summarizes the macros used to print in Rust.

macro Printing Style
print!() prints strings to console
println!() same as print!() but also appends new line character at end of string
eprint!() prints anything within the parentheses as an error
eprintln!() same as eprint!() but also appends new line character at end

Let’s discuss each of the macros in detail.

The print!() macro simply prints the output to the console.

Example

The following example prints “Rust Programming Course” in one line

fn main() {
    print!("Rust Programming");
    print!(" Course");
}

println!()

The println!() macro appends a new line at the end of the string.

Example

The following example prints “Rust Programming” on one line and “Course” on the new line.

fn main() {
    println!("Rust Programming");
    println!("Course");
}

eprint!()

The eprint!() macro displays the output as an error.

Example

The following example prints “Rust Programming” and “Course” on the same line but as an error.

fn main() {
    eprint!("Rust Programming");
    eprint!(" Course");
}


eprintln!()

The eprint!() macro displays the output as an error and appends a new line(\n) at the end of it.

Example

The following example prints “Rust Programming” as an error and appends a new line to it. Then prints “Course” and appends a new line.

fn main() {
    eprintln!("Rust Programming");
    eprintln!("Course");
}


📝Note: eprint!() and eprintln!() come in handy when we want to indicate to the user that an error condition has occurred.

Quiz

  1. Which of the following print statement appends a new line to the output?

a) print!(“Rust”);
b) println!(“Rust”);
c) eprint!(“Rust”);
d) eprintln!(“Rust”);

  1. Which of the following is used to display an error?
    a) eprint!(“Rust”);
    b) eprintln!(“Rust”);
    c) print!(“Rust”);
    d) println!(“Rust”);

  2. What is the output of the following code?
    a) Learning language :
    RustProgrammingLanguage
    b)Learning language :
    Rust
    Programming
    Language