Learn how to print to the console in Rust.
The most common way to write to console in Rust is using println!
:
// This prints "Hello world" to the console
println!("Hello world")
The most common way to write to console in Rust is using println!
:
// This prints "Hello world" to the console
println!("Hello world")
Rust offers other ways to print to console too, like print!
, eprint!
and eprintln!
.
We'll cover these in detail in this concept.
print!
print!
is similar to println!
, but it doesn't append a newline.
// Prints "This is a single line"
print!("This is a single ")
print!("line")
eprint!
and eprintln!
eprint!
and eprintln!
print to stderr (standard error stream) instead of stdout. They're commonly used when printing error messages.
println!("Printing to stdout")
eprintln!("Printing to stderr")
The e
prefix stands for "error".
We covered the following utilities in the Rust standard library:
println!
— Prints a string to console and appends a newline at the endprint!
— Similar to println!
but doesn't append a newline at the endeprintln!
— Similar to println!
but prints to stderr instead of stdouteprint!
— Similar to print!
but prints to stderr instead of stdoutYou can learn more about these in the Formatted print chapter of the Rust book.