Here' s my code
Summary
struct Arr<T: Default, const N: usize> {
arr: [T; N],
}
impl<T: Default + Copy, const N: usize> FromIterator<T> for Arr<T, N> {
fn from_iter<A: IntoIterator<Item=T>>(iter: A) -> Self {
let mut arr = [T::default(); N];
arr.iter_mut().zip(iter.into_iter())
.for_each(|(a, b)| *a = b);
Self { arr }
}
}
Can I use T without Copy trait?
Initially I used this const hack like this: const _ARR: [T; N] = [T::default(); N];
But, it seems to not work since type T cannot be imported from outer function.
Here, N is known at compile time. So, the compiler knows how many times T::default()
should be called. So, I have a feeling it can be done but I wasn't able to find a way to do it.