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 get a capture group using Rust regex?
- How to use regex to match a double quote in Rust?
- How to replace strings using Rust regex?
- How to use non-capturing groups in Rust regex?
- Word boundary example in regex in Rust
- How to use regex to match a group in Rust?
- Example of struct private field in Rust
- How to multiply matrices in Rust?
- How to parse JSON string in Rust?
- How to initialize a Rust HashMap?
See more codes...