Firstly, at least there is a couple of improvements:
- This code can be slightly shorter:
// Using copy_from_slice
self.memory[STARTING_PROGRAM_ADDRESS..][..buffer.len()]
.copy_from_slice(&buffer);
-
skip
can be inefficient as it has to iterate skipped elements. Using the iterator of the mutable slice directly:
// Using zip
for (src, dst) in buffer
.iter()
.zip(&mut self.memory[STARTING_PROGRAM_ADDRESS..])
{
*dst = *src;
}
Index access can be less efficient as an out of bound check has to be done at each access rather than once at the beginning. The generated code can be seen here https://rust.godbolt.org/z/9jdCp4.
In this case, copy_from_slice
, as it is the exact fitting method, is most idiomatic, I think. And using index access is most less idiomatic.