I'm making a rust program and of the things I need is to fill up a string with a specific character to a certain amount defined in an u32 integer. How do I do that?
I assume by character, you mean the rust type char, a.k.a. unicode codepoint. since String implements FromIterator<char>, you can use the RepeatN iterator and collect it into a String, for example:
fn n_char(count: usize, c: char) -> String {
std::iter::repeat_n(c, count).collect()
}
try it on playground:
I found the answer sorry
(I don't know what the solution was for the OP, but for other readers) another possibility depending on the context is to use a format fill character:
let str = "hi";
println!("{str:!<6}");
// hi!!!!
Cool. I like when people ask simple questions that I would not bother to ask myself. I get to learn tricks. I don't do much string handling myself, I find it annoying. All that unicode complexity. I just hide....
I hope to here from @y8v too.
Share what you found please.