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 variablenameand assigns it the value"John".let age = 30;: This line declares a variableageand assigns it the value30.println!("{} is {} years old", name, age);: This line uses theformat!macro to print out a string literal with the variablesnameandageinserted into it. The{}symbols indicate where the variables should be inserted.
Helpful links:
More of Rust
- How to use binary regex in Rust?
- How to use Unicode in a regex in Rust?
- How to match a URL with a regex in Rust?
- How to replace a capture group using Rust regex?
- How to print a Rust HashMap?
- How to use negation in Rust regex?
- How to get size of pointer in Rust
- Regex example to match multiline string in Rust?
- How to replace strings using Rust regex?
- Hashshet example in Rust
See more codes...