rustFormat string arguments in Rust
?
Formatting string arguments in Rust is done using the format! macro. This macro takes a format string and a list of arguments and returns a String object. The format string can contain placeholders for the arguments, which are then replaced with the corresponding values.
Code example:
let name = "John";
let age = 30;
let message = format!("Hello, my name is {}, and I am {} years old.", name, age);
println!("{}", message);
Output
Hello, my name is John, and I am 30 years old.
Explanation of Code Parts:
let name = "John";- This line declares a variable callednameand assigns it the value of the string"John".let age = 30;- This line declares a variable calledageand assigns it the value of the integer30.let message = format!("Hello, my name is {}, and I am {} years old.", name, age);- This line uses theformat!macro to create aStringobject calledmessage. The format string contains two placeholders,{}, which are replaced with the values of the variablesnameandage.println!("{}", message);- This line prints the value of themessagevariable to the console.
Helpful links:
More of Rust
- How to use regex captures in Rust?
- Regex example to match multiline string in Rust?
- How to print a Rust HashMap?
- How to replace a capture group using Rust regex?
- How to lock a Rust HashMap?
- How to join two Rust HashMaps?
- How to match whitespace with a regex in Rust?
- How to replace strings using Rust regex?
- How to create a HashMap of structs in Rust?
- Enum as int in Rust
See more codes...