rustHow to format string literal in Rust
String literals in Rust can be formatted using the format!
macro. This macro allows you to insert variables into a string literal and format them according to the specified format.
Code example:
let name = "John";
let age = 30;
println!("{} is {} years old", name, age);
Output
John is 30 years old
Explanation:
let name = "John";
: This line declares a variablename
and assigns it the value"John"
.let age = 30;
: This line declares a variableage
and assigns it the value30
.println!("{} is {} years old", name, age);
: This line uses theformat!
macro to print out a string literal with the variablesname
andage
inserted into it. The{}
symbols indicate where the variables should be inserted.
Helpful links:
More of Rust
- How to use regex with bytes in Rust?
- How to use regex to match a double quote in Rust?
- How to get all values from a Rust HashMap?
- How to use negation in Rust regex?
- How to match the end of a line in a Rust regex?
- How to use a Rust HashMap in a struct?
- How to iterate through hashmap keys in Rust
- How to get an entry from a HashSet in Rust?
- How to implement PartialEq for a Rust HashMap?
- How to modify an existing entry in a Rust HashMap?
See more codes...