rustFormat string at runtime in Rust
Formatting strings at runtime in Rust can be done using the format!
macro. This macro allows you to create a formatted string using the same syntax as println!
and print!
.
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 theprintln!
macro to print a formatted string. The{}
are placeholders for the values ofname
andage
which are passed as arguments to the macro.
Output
John is 30 years old
Helpful links:
More of Rust
- How to replace a capture group using Rust regex?
- Regex example to match multiline string in Rust?
- How to parse a file with Rust regex?
- How to use regex lookahead in Rust?
- How to use regex captures in Rust?
- How to use regex to match a group in Rust?
- How to match the end of a line in a Rust regex?
- How to perform matrix operations in Rust?
- How to use regex to match a double quote in Rust?
- How to replace strings using Rust regex?
See more codes...