How can I initialize an array using a function? [duplicate]

As of Rust 1.55.0, Sep 2021

You can use array’s .map() method for general functions:

let arr: [Vec<u32>; 10] = [(); 10].map(|_| Vec::with_capacity(100));

(With Rust 1.63.0, Aug 2022 consider using from_fn).

Leave a Comment