Format string into fixed space

⚓ Rust    📅 2025-12-16    👤 surdeus    👁️ 8      

surdeus

Warning

This post was published 51 days ago. The information described in this article may have changed.

I am trying to format a &str into a field that is exactly 7 "characters" wide. I could do the following:

fn fixed_size(inp: &str) -> String {
   let result = inp.to_string();
   result.truncate(7);
   format!("{result:<7}")
}

but (1) that's not exactly right with unicode and (2) seems way more convoluted than it needs to be

1 post - 1 participant

Read full topic

🏷️ Rust_feed