Format number as blank when it's zero

⚓ rust    📅 2025-06-07    👤 surdeus    👁️ 2      

surdeus

I mimic Windows directory out display using something like:

dir.push_str(&format!("{:8}{m:>2}/{d:>2}/{y:4}  {h:>2}:{mm:02} {}M    {:>16} ",' ', pm, metadata.len()));

It works stunning, however if I compare a real dir command display:

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        12/25/2024   5:15 PM                .cargo
-a----        12/29/2024   7:24 PM            100 .gitconfig

There is no number for a directory length, but my format string shows it as 0. How to solve the problem in the shortest and smart way? I suspect also, that I should display zero for empty files. metadata.is_dir() is already stored in some variable.

2 posts - 2 participants

Read full topic

🏷️ rust_feed