[フレーム]

Rust Output (Print Text)


Output (Print Text)

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

Example

println!("Hello World!");
Try it Yourself »

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:

Example

print!("Hello World! ");
print!("I will print on the same line.");
Try it Yourself »

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.");
Try it Yourself »

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!");
Try it Yourself »

Track your progress - it's free!
×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

FORUM ABOUT ACADEMY
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

AltStyle によって変換されたページ (->オリジナル) /