Is there a better ( and stable ) way of initializing long slice behind the box?

Is there a better ( and stable ) way of initializing long slice behind the box?

        let dst_buffer = Box::<[f32]>::new_zeroed_slice(1024);
        let mut dst_buffer = unsafe { dst_buffer.assume_init() };

Plaground
Related question

let mut dst_buffer = vec![0_f32; 1024].into_boxed_slice();

should work fine.

1 Like

Thanks, but 1024 is not constant. It is not known at compile-time variable. Sorry for confusing.

Well, the length in the vec![x; N] macro isn't constant either.

Edit: demonstration; though now, both functions no longer produce exactly the same assembly. (They did with the 1024 in place.) But they shouldn't be different performance-wise anyways.

1 Like

Is not N constant?

Indeed.

let mut dst_buffer = vec![ 0_f32; len ].into_boxed_slice();

Works. Thank you @steffahn

Fix the typo, please

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.