Is there an easier way to fill remaining slots of a vector?

I have a vector that will have a maximum capacity of 256, and sometimes the full capacity will be filled with values, and sometimes some of it will be empty. I know I can use a simple loop to push values on like:

    let mut v: Vec<u8> = String::from("test string").as_bytes().to_vec();
    
    for _i in v.len() .. 256 {
        v.push(0);
    }

but, is this the most efficient way of doing this?

Thanks,
Jeramy

Vec::resize or Vec::resize_with.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.