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 use regex to match a double quote in Rust?
- How to match a URL with a regex in Rust?
- How to match the end of a line in a Rust regex?
- How to use regex with bytes in Rust?
- How to implement PartialEq for a Rust HashMap?
- How to print a Rust HashMap?
- How to use an enum in a Rust HashMap?
- How to perform matrix operations in Rust?
- How to parse JSON string in Rust?
- How to get an element from a HashSet in Rust?
See more codes...