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 get a capture group using Rust regex?
- How to use Unicode in a regex in Rust?
- How to get the last element of a Rust slice?
- How to match the end of a line in a Rust regex?
- How to replace a capture group using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to use an enum in a Rust HashMap?
- How to match whitespace with a regex in Rust?
- How to split a string with Rust regex?
- How to match a URL with a regex in Rust?
See more codes...