Rust Output (Print Text)
Output (Print Text)
To print text in Rust, you can use the println!()
macro:
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!");
Try it Yourself »
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:
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:
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!()
: