rustHow format string with fixed width in Rust
Formatting strings with fixed width in Rust can be done using the format!
macro. This macro allows you to specify the width of the string, as well as other formatting options.
Code example:
let name = "John";
let formatted_name = format!("{:10}", name);
println!("{}", formatted_name);
Output
John
Explanation of code parts:
let name = "John"
: This line declares a variable calledname
and assigns it the value of "John".let formatted_name = format!("{:10}", name)
: This line uses theformat!
macro to format thename
variable with a width of 10 characters.println!("{}", formatted_name)
: This line prints the formatted string to the console.
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...