[AtomicU16::new(0); N]; for const N: usize?

Given const N: usize, is it possible to construct a [AtomicU16::new(0); N] ? I am getting an error about AtomicU16 not implementing copy.

I do want a fixed size array, not a Vec here.

There's a stabilization PR open for what you really want: Stabilize `array_from_fn` by c410-f3r · Pull Request #94119 · rust-lang/rust · GitHub

But in the mean time,

[(); N].map(|()| AtomicU16::new(0))

will work.

2 Likes

Or with the const trick mentioned yesterday:

const ZERO: AtomicU16 = AtomicU16::new(0);

let array = [ZERO; N];
2 Likes

Where was the trick mentioned? I scanned over Vec<T> for T:Default but not Clone - #2 by RobinH but none of the techniques applied.

Not there, in Any way to create a generic static? - #11 by Yandros.