arr_macro icon indicating copy to clipboard operation
arr_macro copied to clipboard

Support for `const` variable in array init

Open JoshMcguigan opened this issue 6 years ago • 5 comments

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

JoshMcguigan avatar Jan 16 '19 12:01 JoshMcguigan

Is there a rust bug associated with this? All the issues linked seem to be closed or archived.

kaimast avatar Jun 29 '20 21:06 kaimast

Is there a rust bug associated with this?

https://github.com/rust-lang/rust/issues/52393

daxpedda avatar Oct 05 '21 12:10 daxpedda

@JoshMcguigan what is a possible workaround?

yegor256 avatar Mar 01 '22 04:03 yegor256

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.

DrRuhe avatar Dec 07 '22 07:12 DrRuhe

Is there a plan to support constant generic?

PENGUINLIONG avatar Apr 14 '24 03:04 PENGUINLIONG