Expand description
§Divide slices into portions of same size
Divide_slice provides two additional methods to the primitive type slice:
divide
: divide a slice inton
non-overlapping portions, returning an iterator.divide_mut
: divide a slice inton
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.
Structs§
- Portion
- An iterator over a slice in
n
non-overlapping portions, starting at the beginning of the slice. - Portion
Mut - An iterator over a slice in
n
mutable non-overlapping portions, starting at the beginning of the slice.