rustHow to format number to string in Rust
Formatting a number to a string in Rust can be 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 special formatting characters that will be replaced with the corresponding argument.
Code example:
let num = 5;
let num_string = format!("The number is {}", num);
Output
The number is 5
Explanation:
let num = 5;
: This line declares a variablenum
and assigns it the value5
.let num_string = format!("The number is {}", num);
: This line uses theformat!
macro to format the number5
into a string. The{}
in the format string is replaced with the value ofnum
.- The output of this code is
The number is 5
.
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...