rustHow to format string in Rust
String formatting in Rust is done using the format!
macro. This macro allows you to create a formatted string with placeholders for values that will be filled in later.
Code example:
let name = "John";
let age = 30;
println!("{} is {} years old", name, age);
Output
John is 30 years old
Explanation of code parts:
let name = "John";
- This line declares a variable calledname
and assigns it the value of "John".let age = 30;
- This line declares a variable calledage
and assigns it the value of 30.println!("{} is {} years old", name, age);
- This line uses theformat!
macro to print out a formatted string with the values ofname
andage
in the appropriate places.
Helpful links:
More of Rust
- How to match a URL with a regex in Rust?
- How to replace a capture group using Rust regex?
- How to clear a Rust HashMap?
- Yield example in Rust
- Example of yield_now in Rust?
- How to convert a Rust slice to a fixed array?
- How to use regex to match a group in Rust?
- How to convert a Rust HashMap to a BTreeMap?
- How to yield a thread in Rust?
- How to replace strings using Rust regex?
See more codes...