Avatar ·

How to pad a string with leading zeros in Rust? Simple way

Hi everyone! I'm starting to learn Rust and came across a task that is very simple in other languages, but here I'm a bit confused. I need to pad a string representing a number with leading zeros to a certain length. For example, I have the number `42`, and I want to get the string `"00042"` (total length 5 characters). In Python I would just write `str(42).zfill(5)`, in C# — `42.ToString("D5")`. How do I do this in Rust? I tried using `format!`, but haven't figured out the syntax yet. Here's my code that doesn't work the way I want: ```rust fn main() { let num = 42; let padded = format!("{:05}", num); // This works for numbers println!("{}", padded); // Prints "00042" } ``` But the problem is that I don't have a number, but a string! Let's say I got the string `"42"`.