rustHow to format string with println in Rust
Rust provides a number of ways to format strings with the println! macro. The most basic way is to use the placeholder syntax, which allows you to insert values into the string. The placeholder syntax uses curly braces {}
to indicate where the value should be inserted.
For example, the following code will print out the string "Hello, world!" with the value of the variable name
inserted into the string:
let name = "world";
println!("Hello, {}!", name);
Output
Hello, world!
Explanation of code parts:
let name = "world";
- This line declares a variable calledname
and assigns it the value of "world".println!("Hello, {}!", name);
- This line uses the println! macro to print out the string "Hello, world!" with the value of thename
variable inserted into the string. The{}
indicates where the value of thename
variable should be inserted.
Helpful links:
More of Rust
- How to get a capture group using Rust regex?
- How to use Unicode in a regex in Rust?
- How to get the last element of a Rust slice?
- How to match the end of a line in a Rust regex?
- How to replace a capture group using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to use an enum in a Rust HashMap?
- How to match whitespace with a regex in Rust?
- How to split a string with Rust regex?
- How to match a URL with a regex in Rust?
See more codes...