rustHow to format string with print 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 callednameand assigns it the value of "John".let age = 30;- This line declares a variable calledageand assigns it the value of 30.println!("{} is {} years old", name, age);- This line uses theformat!macro to print out a string with placeholders for the values ofnameandage. The values ofnameandageare then filled in when the string is printed.
Helpful links:
More of Rust
- How to replace strings using Rust regex?
- How to use regex to match a double quote in Rust?
- How to convert a u8 slice to a hex string in Rust?
- How to insert an element into a Rust HashMap if it does not already exist?
- How to match whitespace with a regex in Rust?
- Regex example to match multiline string in Rust?
- How to use regex lookbehind in Rust?
- How to use regex captures in Rust?
- How to use Unicode in a regex in Rust?
- How to use named capture groups in Rust regex?
See more codes...