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 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 theprintln!macro to print a formatted string. The{}are placeholders for the values ofnameandagewhich are passed as arguments to the macro.
Output
John is 30 years old
Helpful links:
More of Rust
- Generator example in Rust
- How to replace a capture group using Rust regex?
- How to match a URL with a regex in Rust?
- How to use binary regex in Rust?
- How to use regex lookahead in Rust?
- How to make regex case insensitive in Rust?
- Regex example to match multiline string in Rust?
- How to use regex to match a double quote in Rust?
- Yield example in Rust
- How to use a tuple as a key in a Rust HashMap?
See more codes...