Rust Lang

How to Use the println! Macro in the Rust Language

In Rust, the println! is a macro defined in the std::fmt. It is used to handle printing dynamic information to the user. The println! is closely similar to the print! but appends a new line.

Using this guide, we will explore how to parse and print information to the standard output using the Rust println! macro.

Rust println! Macro

The syntax for the println! macro is as shown below:

macro_rules! println {

() => { ... };

($fmt:expr) => { ... };

($fmt:expr, $($arg:tt)*) => { ... };

}

The first argument of the println! macro is the formatted string. This dictates what string to print and how the arguments are parsed as specified by the placeholders.

An example is as shown below:

fn main() {

let name = "Mary";

println!("Hello, {}", name);

}

To insert a placeholder in the println! method, use a pair of curly braces {}. We provide the variable’s name or expression to substitute the provided placeholder outside the string.

In the previous example, the placeholder gets replaced with the value of the variable name.

The previous code should return an output as:

Hello, Mary

The println method supports over one placeholder in a string. An example is as shown below:

fn main() {

let name = "Mary";

let age = 26;

println!("Hello, {}. You are {} years old", name, age);

}

Note that the order of the arguments matters. The first placeholder will be replaced by the name variable and the second placeholder will be replaced by the age variable.

The resulting output is as shown:

Hello, Mary. You are 26 years old

Std::fmt Traits

How the text is displayed using the println! macro is ruled by a set of traits defined in the std::fmt. You can check the documentation to learn more. However, there are two important ones to which to pay attention:

fmt:Display

The Display traits, denoted by the {} placeholder, convert the value to a text when used inside the println method. It formats an output in a user-friendly fashion. Primitive Rust types implement the Display trait. These types include numbers, such as Booleans and strings.

fmt::Debug

The other trait in the std::fmt is the Debug trait. It is denoted by {:?} placeholder and is used to format text in a debugging context. It is also used to print types that do not have a clear user-facing output, such as vectors, arrays, and slices.

An example is as shown below:

let vec = vec![1,2,3];

println!("{:?}", vec)

Conclusion

In this article, we discussed the fundamentals of working with the println! macro in the Rust language. Using the println! macro, we can format strings and include placeholders that can be substituted for the specified variables or expressions. We hope you found this article helpful. Check out the other Linux Hint articles for more tips and information.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list