10 releases (4 stable)

2.1.0 Dec 1, 2022
2.0.1 Jun 24, 2022
2.0.0 Mar 29, 2021
1.1.0 Mar 27, 2021
0.0.2 Mar 17, 2017

#61 in Data structures

Download history 70359/week @ 2024-07-30 75325/week @ 2024-08-06 70427/week @ 2024-08-13 67348/week @ 2024-08-20 67470/week @ 2024-08-27 67675/week @ 2024-09-03 65261/week @ 2024-09-10 65002/week @ 2024-09-17 68855/week @ 2024-09-24 67408/week @ 2024-10-01 69725/week @ 2024-10-08 78537/week @ 2024-10-15 78002/week @ 2024-10-22 72998/week @ 2024-10-29 72945/week @ 2024-11-05 63562/week @ 2024-11-12

301,085 downloads per month
Used in 586 crates (102 directly)

MIT/Apache

22KB
250 lines

array-init

Crates.io Docs

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.

Care is taken not to leak memory shall the initialization fail.

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
});

Minimum Supported Rust Version (MSRV)

array-init will only increase the MSRV on a new major or minor release, but not for patch releases. Any changes of the MSRV will be announced in the changelog. When increasing the MSRV, the new Rust version must have been released at least six months ago. The current MSRV is 1.51.0. MSRV changes can be expected to happen conservatively.

Licensing

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps