divide_slice

Crates.iodivide_slice
lib.rsdivide_slice
version0.1.4
created_at2025-02-15 17:09:47.6168+00
updated_at2025-02-23 09:58:10.183746+00
descriptionProvides methods to divide a slice into portions of same size
homepage
repositoryhttps://github.com/pierretallotte/rust_divide_slice
max_upload_size
id1556951
size11,400
(pierretallotte)

documentation

README

Divide slices into portions of same size

Divide_slice provides two additional methods to the primitive type slice:

  • divide: divide a slice into n non-overlapping portions, returning an iterator.
  • divide_mut: divide a slice into n mutable non-overlapping portions, returning an iterator.

Difference with slice::chunks

The standard library provides the methods chunks and chunks_mut, which return a (mutable) iterator over a given number of elements of a slice at the same time.

The difference between chunks and divide is that you determine the size of the chunks with chunks, and the number of subslices you want with divide:

use divide_slice::Divide;

let slice = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

let mut iter = slice.chunks(3);
assert_eq!(iter.next(), Some(&[1, 2, 3][..]));
assert_eq!(iter.next(), Some(&[4, 5, 6][..]));
assert_eq!(iter.next(), Some(&[7, 8, 9][..]));
assert_eq!(iter.next(), Some(&[10][..]));
assert_eq!(iter.next(), None);

let mut iter = slice.divide(4);
assert_eq!(iter.next(), Some(&[1, 2, 3][..]));
assert_eq!(iter.next(), Some(&[4, 5, 6][..]));
assert_eq!(iter.next(), Some(&[7, 8][..]));
assert_eq!(iter.next(), Some(&[9, 10][..]));
assert_eq!(iter.next(), None);

Divide is more appropriate when you want to split the work equally among different threads.

Commit count: 10

cargo fmt