0

is there any way to concat array literals in rust?

For example, I want an array like [0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 9]

I can define an array [0; 8] and a [6, 9], but is there any way to write this into one definition? The only way I know is writing the whole thing out like

let array: [u8; 10] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 9];

But is there any way then to wrap the zeros? If I for example want 500 instead of 8 zeros?

I only know it's possible to concat the arrays in runtime, and that's also very complicated, but is there like a literal syntax where you can combine to slices?

Thank you

0

1 Answer 1

0

There is no syntax for that, but that is possible, and a macro can make this nicer:

macro_rules! concat_arrays {
    ( $ty:ty, $default:expr => $($arr:expr),* $(,)? ) => {{
        const __CONCAT_ARRAYS__LEN: usize = 0 $( + $arr.len() )*;
        const __CONCAT_ARRAYS__RESULT: [$ty; __CONCAT_ARRAYS__LEN] = {
            let mut result = [$default; __CONCAT_ARRAYS__LEN];
            let mut result_idx = 0;
            $(
                let arr = $arr;
                let mut src_idx = 0;
                while src_idx < arr.len() {
                    result[result_idx] = arr[src_idx];
                    src_idx += 1;
                    result_idx += 1;
                }
            )*
            result
        };
        __CONCAT_ARRAYS__RESULT
    }};
}

let array: [u8; 10] = concat_arrays!(u8, 0 => [0; 8], [6, 9]);

$default is just some temporary value needed for the macro. It can be worked around with more powerful const eval or unsafe code.

Sign up to request clarification or add additional context in comments.

3 Comments

Another possible workaround for the default value would be something like const-default.
Ok thanks, I guess it's not the ideal solution, as it concats the arrays at runtime instead of concatenating the literals at compile time, but I think one can live with that.
@blueSheesh: they are concatenated in const context, which is evaluated at compile-time.