Docs.rs
  • array-init-2.1.0
    • array-init 2.1.0
    • Permalink
    • Docs.rs crate page
    • MIT OR Apache-2.0
    • Links
    • Repository
    • crates.io
    • Source
    • Owners
    • Manishearth
    • Dependencies
    • Versions
    • 100% of the crate is documented
  • Platform
    • i686-pc-windows-msvc
    • i686-unknown-linux-gnu
    • x86_64-apple-darwin
    • x86_64-pc-windows-msvc
    • x86_64-unknown-linux-gnu
  • Feature flags
  • docs.rs
    • About docs.rs
    • Privacy policy
  • Rust
    • Rust website
    • The Book
    • Standard Library API Reference
    • Rust by Example
    • The Cargo Guide
    • Clippy Documentation

Crate array_init

array_init2.1.0

  • All Items

Sections

  • Examples:

Crate Items

  • Functions

Crates

  • array_init

Crate array_init

Source
Expand description

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: [u32; 50] = array_init::array_init(|i: usize| (i * i) as u32);

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

Functions§

array_init
Initialize an array given an initializer expression.
from_iter
Initialize an array given an iterator
from_iter_reversed
Initialize an array in reverse given an iterator
map_array_init
Initialize an array given a source array and a mapping expression. The size of the source array is the same as the size of the returned array.
try_array_init
Initialize an array given an initializer expression that may fail.

Results

Settings
Help
    extern crate
    array_init
    The array-init crate allows you to initialize arrays with …
    function
    array_init::array_init
    Initialize an array given an initializer expression.
    function
    array_init::map_array_init
    Initialize an array given a source array and a mapping …
    function
    array_init::try_array_init
    Initialize an array given an initializer expression that …
No results :(
Try on DuckDuckGo?

Or try looking in one of these:
  • The Rust Reference for technical details about the language.
  • Rust By Example for expository code examples.
  • The Rust Book for introductions to language features and the language itself.
  • Docs.rs for documentation of crates released on crates.io.
No results :(
Try on DuckDuckGo?

Or try looking in one of these:
  • The Rust Reference for technical details about the language.
  • Rust By Example for expository code examples.
  • The Rust Book for introductions to language features and the language itself.
  • Docs.rs for documentation of crates released on crates.io.