Function std::array::from_fn[][src]

pub fn from_fn<F, T, const N: usize>(cb: F) -> [T; N] where
    F: FnMut(usize) -> T, 
🔬 This is a nightly-only experimental API. (array_from_fn #89379)
Expand description

创建一个数组 [T; N],其中每个数组元素 T 都由 cb 调用返回。

Arguments

  • cb: 回调,其中传入的参数是当前数组索引。

Example

#![feature(array_from_fn)]

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