Start learning free

LESSON 1 OF 414 · WELCOME TO RUST

Printing to the Console in Rust with println!

Lesson Read the full explanation

TL ; DR:

  • The console is a panel that displays important messages, like errors, for developers.


  • In Rust, we use println! to print something to the console. For example, this program:

    println!("{}", 13);
    

    prints 13 to the console.



Full lesson:

The console is a panel that displays important messages, like errors, for developers.

Much of the work the computer does with our code is invisible to us by default. If we want to see things appear on our screen, we can print to our console directly.


println!:

In Rust, we use println! to print something to the console. For example, this program:

println!("{}", 13);

prints 13 to the console.

When you run this program, this is what you'll see in your console:

13

We refer to the data printed to the console as the output of the program.


End of line:

In Rust, println! automatically adds a newline at the end of whatever it prints.

This newline moves the cursor to the next line in the console. It's like pressing Enter in your text editor to move to the next line.

We usually use println! when we want to print multiple values on separate lines:

println!("{}", 1);
println!("{}", 2);
println!("{}", 3);

The output of this program would be:

1
2
3

Assignment
Follow the Coding Tutorial and let's print some values to the console!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    RUST · WELCOME TO RUST

    Print in Rust, explained

    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:

    • println is 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 lets println! 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

    • \n inside a string is a newline, and \t is a tab.
    • To print a double quote inside the text, escape it: println!("She said \"hi\"") prints She said "hi".
    • Because { and } mean something to println!, print a literal brace by doubling it: println!("{{}}") prints {}.

    Common mistakes

    • Leaving out the !. println("hi") fails with "expected function, found macro println", and the compiler suggests using !.
    • Passing a number without a format string. Use println!("{}", 1998), not println!(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:

    1. Write the macro name with its !: println!.
    2. Open the parentheses and write the format string "{}" as the first argument.
    3. Add a comma and the value to print, 1998.
    4. 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, and print! 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.
    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free