Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I split a vector into smaller vectors of size N?

Tags:

vector

rust

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);
like image 864
Antonin GAVREL Avatar asked Nov 28 '25 03:11

Antonin GAVREL


2 Answers

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

like image 105
Denys Séguret Avatar answered Nov 29 '25 17:11

Denys Séguret


There is a method already existing for slices:

pub fn chunks(&self, chunk_size: usize) -> Chunks<'_, T>

Returns an iterator over chunk_size elements of the slice at a time, starting at the beginning of the slice. The chunks are slices and do not overlap. If chunk_size does not divide the length of the slice, then the last chunk will not have length chunk_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.

like image 35
Antonin GAVREL Avatar answered Nov 29 '25 17:11

Antonin GAVREL