I would like to divide a single owned array into two owned halves—two separate arrays, not slices of the original array. The respective sizes are compile time constants. Is there a way to do that without copying/cloning the elements?
let array: [u8; 4] = [0, 1, 2, 3];
let chunk_0: [u8; 2] = ???;
let chunk_1: [u8; 2] = ???;
assert_eq!(
[0, 1],
chunk_0
);
assert_eq!(
[2, 3],
chunk_1
);
Since it would amount to merely moving ownership of the elements, I have a hunch there should be a zero-cost abstraction for this. I wonder if I could do something like this with some clever use of transmute and forget. But there are a lot of scary warnings in the docs for those functions.
My main motivation is to operate on large arrays in memory without as many mem copies. For example:
let raw = [0u8; 1024 * 1024];
let a = u128::from_be_array(???); // Take the first 16 bytes
let b = u64::from_le_array(???); // Take the next 8 bytes
let c = ...
The only way I know to accomplish patterns like the above is with lots of mem copying which is redundant.