What is Print?
The println!() macro is how your Rust program talks to you! It displays text on the screen.
Printing Text
To print text, put it inside quotes and wrap it with println!():
rust
fn main() {
println!("Hello!");
}
This shows:
text
Hello!
Notice the ! after println -- that means it's a macro, which is a special kind of command in Rust. Don't forget it!
Printing Numbers
You can print numbers by using {} as a placeholder:
rust
fn main() {
println!("{}", 42);
println!("{}", 3.14);
}
Printing Multiple Things
Use multiple {} placeholders to print several things on one line:
rust
fn main() {
println!("I am {} years old", 10);
}
Output:
text
I am 10 years old
You can use as many placeholders as you want:
rust
fn main() {
println!("{} + {} = {}", 2, 3, 5);
}
Output:
text
2 + 3 = 5
Multiple Lines
Each println!() starts a new line:
rust
fn main() {
println!("Line 1");
println!("Line 2");
println!("Line 3");
}
Blank Lines
Call println!() with nothing inside the quotes to print a blank line:
rust
fn main() {
println!("Top");
println!();
println!("Bottom");
}
Output:
text
Top Bottom
Now let's practice!