thomas-shirley.com

QuickTips: The println! macro

This macro is a powerful function to output messages to the shell. Here is how to use it and how the formatting parameters are used.

First up, here's the basic usage of the println! function in Rust.

let x = 1;

println!("X is {}", x)
X is 1

We are assigning the variable x with the u32 integer 1.

Rust's default behaviour when assigning an integer to a variables is to give it the u32 datatype.

Using the same variable multiple times in a println! macro

To use a variable twice within a println! statement, the values are referenced within the print statement by using numerical references to the variables passed into the function. The references start at 0:

let x = 1;

println!("Value X is {0}, it is also {0}", x);
Value X is 1, it is also 1
06-11-2022