C-like array initialization where only non-0 elements need to be specified


#1

I need to create a const array that has only the first few elements non-0, and the rest should be filled with 0s. In C I can do:

const arr[100] = {3,2,1};

but I can’t figure out Rust equivalent. I’ve tried:

const arr:[u8; 100] = [3,2,1, ..Default::default()];

but it tries to put Range object in the array.


#2

Sadly there is no support for this, but you can write a macro. I wrote one in the past.


#3

It’s not convenient, but include_bytes! is another possibility.