Divide

Trait Divide 

Source
pub trait Divide<T> {
    // Required methods
    fn divide(&self, n: usize) -> Portion<'_, T> ;
    fn divide_mut(&mut self, n: usize) -> PortionMut<'_, T> ;
}

Required Methods§

Source

fn divide(&self, n: usize) -> Portion<'_, T>

Source

fn divide_mut(&mut self, n: usize) -> PortionMut<'_, T>

Implementations on Foreign Types§

Source§

impl<T> Divide<T> for [T]

Source§

fn divide(&self, n: usize) -> Portion<'_, T>

Divides a slice into n non-overlapping portions, returning an iterator.

The portions are computed by distributing elements as evenly as possible. If the length of the slice is not evenly divisible by n, the first portions may have one more element than the others. If the length of the slice is smaller than n, the last portions will be empty.

§Panics

Panics if n is 0.

§Example
use divide_slice::Divide;

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

fn divide_mut(&mut self, n: usize) -> PortionMut<'_, T>

Divides a slice into n mutable non-overlapping portions, returning an iterator.

The portions are computed by distributing elements as evenly as possible. If the length of the slice is not evenly divisible by n, the first portions may have one more element than the others. If the length of the slice is smaller than n, the last portions will be empty.

§Panics

Panics if n is 0.

§Example
use divide_slice::Divide;

let mut slice = [1, 2, 3, 4, 5, 6];
slice.divide_mut(3).for_each(|e| e[0] += 1);
let mut iter = slice.divide_mut(3);
assert_eq!(iter.next(), Some(&mut [2, 2][..]));
assert_eq!(iter.next(), Some(&mut [4, 4][..]));
assert_eq!(iter.next(), Some(&mut [6, 6][..]));
assert_eq!(iter.next(), None);

Implementors§