arr_macro
arr_macro copied to clipboard
Support for `const` variable in array init
Currently, arr_macro
does not support const
variables in array initialization as shown below. This is because the macro needs to know the exact value of SIZE
, but the compiler expands macros before doing name resolution.
const SIZE: usize = 3;
arr![None; SIZE];
See additional details at dtolnay/syn#101
Is there a rust bug associated with this? All the issues linked seem to be closed or archived.
Is there a rust bug associated with this?
https://github.com/rust-lang/rust/issues/52393
@JoshMcguigan what is a possible workaround?
I ran into this while using const generics to specify an array size and trying to fill that array with None without T being Copy or Default. I solved it by using this snippet:
let array: [Option<T>; N] = (0..N).map(|_| None).collect::<Vec<_>>().try_into().ok().unwrap();
The .ok()
is needed to relax the Debug
trait bound on T
.
It is obviously not ideal but for my purposes a good enough workaround.
Is there a plan to support constant generic?