bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Rust/Rust Tutorial
Rust•Rust Tutorial

Rust Output (Print Text)

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Rust Output (Print Text)?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___!("Hello World!");
3Order

Put the learning moves in the order that makes the concept easiest to apply.

To print text in Rust, you can use the println!
Add New Lines Manually
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