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 calledname
and assigns it the value of the string"John"
.let age = 30;
- This line declares a variable calledage
and 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 aString
object calledmessage
. The format string contains two placeholders,{}
, which are replaced with the values of the variablesname
andage
.println!("{}", message);
- This line prints the value of themessage
variable to the console.
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...