from_fn in std::array - Rust
Description: Creates an array where each element is produced by calling `f` with that element’s index while walking forward through the array.

## from_fn

# Function from_fn

1.63.0 (const: unstable) · Source

    pub fn from_fn<T, const N: usize, F>(f: F) -> [T; N]where
        F: FnMut(usize) -> T,

Expand description

Creates an array where each element is produced by calling `f` with that element’s index while walking forward through the array.

This is essentially the same as writing

    [f(0), f(1), f(2), …, f(N - 2), f(N - 1)]

and is similar to `(0..i).map(f)`, just for arrays not iterators.

If `N == 0`, this produces an empty array without ever calling `f`.

## §Example

    // type inference is helping us here, the way `from_fn` knows how many
    // elements to produce is the length of array down there: only arrays of
    // equal lengths can be compared, so the const generic parameter `N` is
    // inferred to be 5, thus creating array of 5 elements.

    let array = core::array::from_fn(|i| i);
    // indexes are:    0  1  2  3  4
    assert_eq!(array, [0, 1, 2, 3, 4]);

    let array2: [usize; 8] = core::array::from_fn(|i| i * 2);
    // indexes are:     0  1  2  3  4  5   6   7
    assert_eq!(array2, [0, 2, 4, 6, 8, 10, 12, 14]);

    let bool_arr = core::array::from_fn::<_, 5, _>(|i| i % 2 == 0);
    // indexes are:       0     1      2     3      4
    assert_eq!(bool_arr, [true, false, true, false, true]);

You can also capture things, for example to create an array full of clones where you can’t just use `[item; N]` because it’s not `Copy`:

    let my_string = String::from("Hello");
    let clones: [String; 42] = std::array::from_fn(|_| my_string.clone());
    assert!(clones.iter().all(|x| *x == my_string));

The array is generated in ascending index order, starting from the front and going towards the back, so you can use closures with mutable state:

    let mut state = 1;
    let a = std::array::from_fn(|_| { let x = state; state *= 2; x });
    assert_eq!(a, [1, 2, 4, 8, 16, 32]);