leshow
leshow

Reputation: 1648

generating string of set length

I'm trying to generate a string such that if there's a list like [16,24..] and another list ["00","7F"..]

the generated list is [00000000000000000000000000000000,7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F, ...

in python the code would be:

    for l in range(16, 32 + 1, 8):
        for b in ['00', '7f', '80', 'ff']:
          do_something(b * l);

I'm uncertain how to accomplish this in rust, thus far I've got:

for &i in [16u,24u,32u].iter() {
    for n in ["00","7f","80","ff"].iter() {

    }
}

but i have no idea how to create the strings.

Upvotes: 2

Views: 274

Answers (1)

Dogbert
Dogbert

Reputation: 222188

You can use repeat wich was in it's own trait (std::str::StrAllocating), but nowadays is directly implemented on str directly:

fn main() {
    for &i in [16u, 24u, 32u].iter() {
        for n in ["00", "7f", "80", "ff"].iter() {
            println!("{}", n.repeat(i))
        }
    }
}

prints

00000000000000000000000000000000
7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f
80808080808080808080808080808080
ffffffffffffffffffffffffffffffff
000000000000000000000000000000000000000000000000
7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f
808080808080808080808080808080808080808080808080
ffffffffffffffffffffffffffffffffffffffffffffffff
0000000000000000000000000000000000000000000000000000000000000000
7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f
8080808080808080808080808080808080808080808080808080808080808080
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

Demo

Upvotes: 4

Related Questions