How to Pad a String in Rust

Pad a string in Rust using the format! macro with alignment specifiers like < for left and > for right padding.

Use the format! macro with the {0:<width} syntax to left-pad or {0:>width} to right-pad a string. This creates a new string with the original text aligned and filled with spaces to the specified width.

let text = "hello";
let padded_left = format!("{0:<10}", text); // "hello     "
let padded_right = format!("{0:>10}", text); // "     hello"