Rust Output
Showing text on the screen is the first thing most programs do, and it is how you will check that your code works. Rust prints output with a family of macros: println!, print! and a few relatives.
println! and print!
println! prints text and then moves to a new line. print! prints the text but stays on the same line, so the next output continues right after it.
fn main() {
print!("Loading");
print!("...");
println!(" done!");
println!("Ready.");
}Loading... done!
Ready.
You can also put \n inside a string to start a new line yourself. println!() with no arguments prints an empty line.
fn main() {
println!("Line one\nLine two");
println!();
println!("After a blank line");
}Line one
Line two
After a blank line
Why the exclamation mark?
println! is a macro, not a function. A macro is code that writes more code at compile time. Printing is a macro because it accepts any number of arguments and checks the format string while compiling: if your placeholders and arguments do not match, the program will not build.
Placeholders
Curly braces {} inside the string are placeholders. Each one is replaced by the next argument, in order:
fn main() {
let name = "Ada";
let age = 36;
println!("{} is {} years old.", name, age);
// Variables can also go directly inside the braces
println!("{name} wrote the first program.");
// Positional arguments can be reused
println!("{0} + {0} = {1}", 5, 10);
}Ada is 36 years old.
Ada wrote the first program.
5 + 5 = 10
Formatting options
After a colon inside the braces you can control how a value looks:
| Placeholder | Effect | Example output (brackets show the width) |
|---|---|---|
{:.2} |
2 digits after the decimal point | 3.14 |
{:5} |
Width of at least 5 characters | [ 42] |
{:<8} |
Left-align in 8 characters | [ab ] |
{:>8} |
Right-align in 8 characters | [ ab] |
{:^8} |
Center in 8 characters | [ ab ] |
{:05} |
Pad with zeros to width 5 | 00042 |
{:b} / {:x} |
Binary / hexadecimal | 101010 / 2a |
{:?} |
Debug format (for developers) | "hi" with quotes |
These are very handy for printing tidy tables:
fn main() {
let price = 4.5;
println!("Price: ${:.2}", price);
println!("[{:<6}|{:^6}|{:>6}]", "left", "mid", "right");
println!("Order #{:05}", 42);
println!("42 in binary is {:b}, in hex is {:x}", 42, 42);
println!("Debug view: {:?}", "text");
}Price: $4.50
[left | mid | right]
Order #00042
42 in binary is 101010, in hex is 2a
Debug view: "text"
Printing braces and quotes
Because { and } have a special meaning, write them twice to print them literally. Use \" for a double quote inside a string:
fn main() {
println!("Use {{}} as a placeholder.");
println!("She said \"hello\".");
}Use {} as a placeholder.
She said "hello".
Other output macros
| Macro | What it does |
|---|---|
println! |
Prints to standard output with a newline |
print! |
Prints to standard output without a newline |
eprintln! / eprint! |
Prints to standard error, used for error messages |
format! |
Builds a String instead of printing it |
Tip:
format!uses exactly the same placeholders asprintln!. Use it when you want to keep the text, for examplelet msg = format!("Hi {}", name);.
Test yourself
What is the difference between print! and println!?
print!is fasterprintln!adds a newline at the endprint!only prints numbers- There is no difference
What does println!("{:.1}", 2.75); print?
- 2.75
- 2.7
- 2.8
- 3
How do you print a literal { with println!?
- \{
- {{
- '{'
- {}