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 regex to match a double quote in Rust?
- How to create a HashMap of structs in Rust?
- How to replace a capture group using Rust regex?
- How to match the end of a line in a Rust regex?
- How to modify an existing entry in a Rust HashMap?
- How do I identify unused variables in Rust?
- How to use negation in Rust regex?
- Regex example to match multiline string in Rust?
- How to use a Rust HashMap in a struct?
- How do I use a variable number of arguments in Rust?
See more codes...