Trait init_with::InitWith [] [src]

pub trait InitWith<T> {
    fn init_with<F>(init: F) -> Self where F: FnMut() -> T, Self: Sized;
}

A trait that allows you to create an instance of a type by using a given function to generate each element.

Required Methods

Create a new instance of this type using the given function to fill elements.

Examples

Prefilling an array with a Vec, with no unsafe code*:

use init_with::InitWith;

let src = vec![1, 2, 3];
let dest: [i32; 3] = {
    let mut idx = 0;

    //*okay, there's unsafe code in here, but you didn't have to write it
    <[i32; 3]>::init_with(|| {
        let val = src[idx];
        idx += 1;
        val
    })
};

assert_eq!(src, dest);

Implementors