rustHow to format string with print in Rust
String formatting in Rust is done using the format!
macro. This macro allows you to create a formatted string with placeholders for values that will be filled in later.
Code example:
let name = "John";
let age = 30;
println!("{} is {} years old", name, age);
Output
John is 30 years old
Explanation of code parts:
let name = "John";
- This line declares a variable calledname
and assigns it the value of "John".let age = 30;
- This line declares a variable calledage
and assigns it the value of 30.println!("{} is {} years old", name, age);
- This line uses theformat!
macro to print out a string with placeholders for the values ofname
andage
. The values ofname
andage
are then filled in when the string is printed.
Helpful links:
More of Rust
- How to get a capture group using Rust regex?
- How to use regex with bytes in Rust?
- How to match the end of a line in a Rust regex?
- Hashshet example in Rust
- How to create a new Rust HashMap with values?
- How do I use a variable from another file in Rust?
- How to get the length of a Rust HashMap?
- How to get an element from a HashSet in Rust?
- How to implement PartialEq for a Rust HashMap?
- How to convert the keys of a Rust HashMap to a vector?
See more codes...