We can use println! to print texts on the screen.
For example:
fn main() {
println!("Hello");
}This prints Hello on the screen.
Hello
The text put in quotes "" is called a string.
println! helps us to print strings on the screen.
In addition, we can use {} to indicate a string that will be specified later.
For example:
fn main() {
println!("{}", "Hello");
}In the code, we tell the compiler that we need to print something, and this something is Hello.
The output is the same.
Hello
The symbol {} is useful especially when we have more than one text.
fn main() {
println!("{} {}", "Hello", "world");
}This outputs:
Hello world
We can format a printed text in the following way.
fn main() {
println!("{}, {}!", "Hello", "world");
}Output:
Hello, world!
➡️ Next: Printing Special Strings
📘 Back: Table of contents