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 match a URL with a regex in Rust?
- How to replace all matches using Rust regex?
- How to ignore case in Rust regex?
- How to match the end of a line in a Rust regex?
- How to replace a capture group using Rust regex?
- How to get a capture group using Rust regex?
- How to use regex captures in Rust?
- How to use named capture groups in Rust regex?
- How to use regex with bytes in Rust?
- How to use regex to match a group in Rust?
See more codes...