6 releases

✓ Uses Rust 2018 edition

new 0.1.1 Nov 15, 2019
0.1.0 Jul 23, 2019
0.0.4 Jul 23, 2018
0.0.3 Apr 25, 2018
0.0.2 Mar 17, 2017

#1 in #initialization

Download history 804/week @ 2019-08-02 1154/week @ 2019-08-09 1205/week @ 2019-08-16 1021/week @ 2019-08-23 1277/week @ 2019-08-30 2065/week @ 2019-09-06 1983/week @ 2019-09-13 1725/week @ 2019-09-20 1536/week @ 2019-09-27 1848/week @ 2019-10-04 1329/week @ 2019-10-11 1401/week @ 2019-10-18 1626/week @ 2019-10-25 1861/week @ 2019-11-01 1419/week @ 2019-11-08

6,729 downloads per month
Used in 22 crates (5 directly)

MIT/Apache

15KB
252 lines

array-init

Documentation

Crates.io

(still kinda experimental, API may change, may be secretly unsafe)

The array-init crate allows you to initialize arrays with an initializer closure that will be called once for each element until the array is filled.

This way you do not need to default-fill an array before running initializers. Rust currently only lets you either specify all initializers at once, individually ([a(), b(), c(), ...]), or specify one initializer for a Copy type ([a(); N]), which will be called once with the result copied over.

Examples:

// Initialize an array of length 50 containing
// successive squares

let arr: [usize; 50] = array_init::array_init(|i| i * i);

// Initialize an array from an iterator
// producing an array of [1,2,3,4] repeated

let four = [1,2,3,4];
let mut iter = four.iter().copied().cycle();
let arr: [u32; 50] = array_init::from_iter(iter).unwrap();

// Closures can also mutate state. We guarantee that they will be called
// in order from lower to higher indices.

let mut last = 1u64;
let mut secondlast = 0;
let fibonacci: [u64; 50] = array_init::array_init(|_| {
    let this = last + secondlast;
    secondlast = last;
    last = this;
    this
});

No runtime deps