I want to be able to do this:
let mut my_array: [MyType; 10] = [MyType::new(1, 2, 3, 4); 10];
when MyType isn't copyable, i.e. I want it to call the constructor for each element rather than calling it once and trying to copy. Is this possible?
Is it also possible to include the array index in the constructor call:
let mut my_array: [MyType; 10] = [MyType::new(_index, 2, 3, 4); 10];
so that my array is initialized with MyType:new(1,2,3,4), MyType:new(2,2,3,4), MyType:new(3,2,3,4), etc.
The array-init library provides a safe interface to do this:
let mut my_array: [MyType; 10] = array_init(|i| MyType::new(i, 2, 3, 4));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With