Reputation: 2343
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.
Upvotes: 3
Views: 784
Reputation: 1064
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));
Upvotes: 3