To the best of my knowledge, there is no such shortcut. You do have a few options, though.
The direct syntax
The direct syntax to initialize an array works with Copy types (integers are Copy):
let array = [0; 1024];
initializes an array of 1024 elements with all 0s.
Based on this, you can afterwards modify the array:
let array = {
let mut array = [0; 1024];
array[0] = 7;
array[1] = 8;
array
};
Note the trick of using a block expression to isolate the mutability to a smaller section of the code; we'll reuse it below.
The slice syntax
There is a shorter form, based on clone_from_slice.
let array = {
let mut array = [0; 32];
// Override beginning of array.
array[..2].clone_from_slice(&[7, 8]);
array
};
The iterator syntax
There is also support to initialize an array from an iterator:
let array = {
let mut array = [0; 1024];
for (i, element) in array.iter_mut().enumerate().take(2) {
*element = (i + 7);
}
array
};
And you can even (optionally) start from an uninitialized state, using an unsafe block:
#![feature(maybe_uninit_uninit_array)]
let array = {
// Create an uninitialized array.
let mut array: [MaybeUninit<i32>; 10] = MaybeUninit::uninit_array();
let nonzero = 2;
for (i, element) in array.iter_mut().enumerate().take(nonzero) {
// Overwrite `element` without running the destructor of the old value.
element.write(i + 7)
}
for element in array.iter_mut().skip(nonzero) {
// Overwrite `element` without running the destructor of the old value.
element.write(0)
}
// Safety:
// - All elements have been initialized.
unsafe { MaybeUninit::array_assume_init(array) }
};