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
- Regex example to match multiline string in Rust?
- How to map a Rust slice?
- How to replace a capture group using Rust regex?
- How to use regex captures in Rust?
- How to create a HashMap of structs in Rust?
- How to use non-capturing groups in Rust regex?
- How to match the end of a line in a Rust regex?
- How to use regex with bytes in Rust?
- How to perform matrix operations in Rust?
- How to use regex lookbehind in Rust?
See more codes...