What printing is and why you need it
The console is the text area where a program shows its output. Most of what a program does stays invisible: variables change, calculations happen, and nothing appears on screen. To see a value, you tell the program to print it. Printing is also the simplest debugging tool: when a result looks wrong, print the values that led to it.
In Rust, printing is done with the println! macro:
println!("{}", 13);
This prints:
13
Anatomy of println!
The call has three parts:
printlnis the name of the macro.- The
!after the name marks it as a macro rather than an ordinary function. Macros are expanded by the compiler before the code is built, which is what letsprintln!accept any number of arguments of different types. Leaving out the!is a mistake that the compiler catches. - Inside the parentheses, the first argument is a format string. Each
{}in it is a placeholder, replaced by the next argument.
In println!("{}", 13), the format string is "{}", and the placeholder takes the value 13. The format string is required. Writing println!(13) doesn't compile: the compiler reports "format argument must be a string literal".
Printing text
When you want to print plain text, the text itself can be the format string:
println!("Hello");
The quotes are not printed; they only mark where the text starts and ends.
Several values at once
Each placeholder takes the next argument, in order:
println!("{} + {} = {}", 2, 3, 2 + 3);
2 + 3 = 5
The number of placeholders must match the number of arguments. Both of these are compile errors:
println!("{}");reports "1 positional argument in format string, but no arguments were given".println!("{}", 1, 2);reports "argument never used".
Variables can also be placed straight into the format string by name, as in println!("{name} is {age}"), which works for a plain variable name. Arbitrary expressions such as 2 + 3 still go in the argument list.
The end of the line
println! adds a newline after whatever it prints, like pressing Enter. This is why separate calls produce separate lines:
println!("{}", 1);
println!("{}", 2);
println!("{}", 3);
1
2
3
If you don't want the newline, use print!. It works the same way but stays on the same line:
print!("a");
print!("b");
println!();
ab
The final println!(), with no arguments, just ends the line.
Special characters
\ninside a string is a newline, and\tis a tab.- To print a double quote inside the text, escape it:
println!("She said \"hi\"")printsShe said "hi". - Because
{and}mean something toprintln!, print a literal brace by doubling it:println!("{{}}")prints{}.
Common mistakes
- Leaving out the
!.println("hi")fails with "expected function, found macroprintln", and the compiler suggests using!. - Passing a number without a format string. Use
println!("{}", 1998), notprintln!(1998). - Forgetting the semicolon. A
println!in the middle of a block needs its;to end the statement. - Mismatched placeholders and arguments. Count the
{}and the values.
The lesson's exercise
The exercise asks you to print the number 1998. Follow the anatomy above:
- Write the macro name with its
!:println!. - Open the parentheses and write the format string
"{}"as the first argument. - Add a comma and the value to print,
1998. - Close the parenthesis and end the line with a semicolon.
println!("{}", 1998);
It prints:
1998
Key takeaways
- Use
println!to print a value followed by a newline, andprint!to print without the newline. - The
!is part of the name: it marks a macro. - The first argument is a format string, and every
{}in it is replaced by the next argument. - The number of placeholders must match the number of arguments.
