bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Rust/Rust Tutorial
Rust•Rust Tutorial

Rust Output (Print Text)

Output (Print Text)

To print text in Rust, you can use the println!() macro:

Example

println!("Hello World!");

You can use as many println!() macros as you want. Note that it will add a new line for each macro:

Example

println!("Hello World!");
println!("I am learning Rust.");
println!("It is awesome!");

The Print Macro

There is also a print!() macro, which is similar to println!() .

The only difference is that it does not insert a new line at the end of the output:

Example

print!("Hello World! ");
print!("I will print on the same line.");

Note that we add an extra space when needed (after "Hello World!" in the example above), for better readability.

In this tutorial, we will only use println!() as it makes it easier to read the output of code.

Add New Lines Manually

If you really want to add a new line in print!() , you can use the \n character:

Example

print!("Hello World!\n");
print!("I will print on the same line.");

Just like with many other programming languages, you can use the newline character ( \n ) to break up lines. It is an escape sequence , and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.

You can also break up a line in the middle of a sentence. This goes for both print!() and println!() :

Example

println!("Hello World!\nThis line was broken up!");

Previous

Rust Syntax

Next

Rust Comments

This chapter

Overview
20

Lessons

46m

Read time

1. Rust Tutorial2. Rust Introduction3. Rust Get Started4. Rust Syntax5. Rust Output (Print Text)6. Rust Comments7. Rust Variables8. Rust Data Types9. Rust Constants10. Rust Operators11. Rust Booleans12. Rust If .. Else Conditions13. Rust Match14. Rust Loops15. Rust While Loops16. Rust For Loop17. Rust Functions18. Rust Scope19. Rust Strings20. Rust Ownership

On this page

Output (Print Text)The Print MacroAdd New Lines Manually