How to split a vector
let v: Vec<u8>; // vector with size x
into a vector of vectors of maxsize n? Pseudocode:
let n: usize = 1024;
let chunks_list: Vec<Vec<u8>> = chunks(v, n);
or using slices (to avoid copying):
let v: &[u8];
let chunks_list: Vec<&[u8]> = chunks(v, n);
Rust slices already contain the necessary method for that: chunks.
Starting from this:
let src: Vec<u8> = vec![1, 2, 3, 4, 5];
you can get a vector of slices (no copy):
let dst: Vec<&[u8]> = src.chunks(3).collect();
or a vector of vectors (slower, heavier):
let dst: Vec<Vec<u8>> = src.chunks(3).map(|s| s.into()).collect();
playground
There is a method already existing for slices:
pub fn chunks(&self, chunk_size: usize) -> Chunks<'_, T>Returns an iterator over
chunk_sizeelements of the slice at a time, starting at the beginning of the slice. The chunks are slices and do not overlap. Ifchunk_sizedoes not divide the length of the slice, then the last chunk will not have lengthchunk_size.
There is also chunks_mut for mutability as well as chunks_exact and chunks_exact_mut if the last chunk has to respect the size n, along with the unsafe as_chunks_unchecked in case we assume there is no remainder, see below example:
fn main() {
let v: [u8; 5] = *b"lorem";
let n = 2;
let chunks = v.chunks(n);
let chunks_list: Vec<&[u8]> = chunks.collect();
println!("{:?}", chunks_list);
}
Using a slice instead of vectors has some benefits, notably avoiding the overhead of copying.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With