The Rust println!
macro is a built-in macro that is used to print text to stdout (standard output).
Here is an example code that uses the println!
macro to print a simple message to the console:
fn main() {
println!("Hello, World!");
}
In this example, we call the println!
macro with one argument: the string "Hello, World!"
. The macro then prints the string to the console.
The println!
macro is similar to the print!
macro, but adds a newline character (\n
) to the end of the output, while the print!
macro does not.
The println!
macro can also accept additional arguments for string formatting. For example, we can use the {}
placeholder to insert variables or values into the output string:
fn main() {
let name = "Alice";
let age = 30;
println!("My name is {} and age is {}", name, age);
}
In this example, we use two placeholders ({}
) in the output string to print the values of the name
and age
variables. When the macro is executed, it replaces the {}
placeholders with the corresponding values ("Alice"
and 30
, respectively). The resulting output would be:
My name is Alice and age is 30
The println!
macro is similar to the format!
macro for formatting strings, and the write!
macro for writing formatted data into a buffer.