I have an odd case where I want to initialize some segments of an array as copies of an existing array, and call a function to initialize the other elements. Naively, I'd like to do something like this:
fn build_array(input: [char; 8]) -> [char; 25] {
let mut out: [char; 25];
out[6..10].copy_from_slice(input[0..4]);
out[16..20].copy_from_slice(input[4..8]);
for i in 0..25 {
if (6..10).contains(i) || (16..20).contains(i) {
continue;
}
out[i] = some_func();
}
}
Obviously I could just initialize the array, but that would be inefficient. I was surprised to find that wrapping the copy_from_slice()
calls in an unsafe block does not make this compile. Creating multiple array segments and concatenating them doesn't seem to simplify things based on this question.
Does anyone know of an idiomatic and efficient way to accomplish what I want to do here?
Edit: some_func()
here is meant as a placeholder, the elements that aren't provided in input
don't all have the same value.