How do you copy between arrays of different sizes in Rust?

Manually one can do for (dst, src) in array1.iter_mut().zip(&array2) { *dst = *src } for a typical slice. However, there is a likely faster specialization in clone_from_slice: dst[..4].clone_from_slice(&src) A slightly older method is to use std::io::Write, which was implemented for &mut [u8]. use std::io::Write; let _ = dst.write(&src) This will write up to the end … Read more

Simple VBA array join not working

The cookie goes to brettdj as resizing a 1D array and populating it is the best way to go (fastest) but I wanted to offer a more lesser-known compact solution in the case that you don’t plan to use this on long arrays. It’s not as fast than the 1D approach, but not slow like … Read more

Is there any way to allocate a standard Rust array directly on the heap, skipping the stack entirely?

Summary: your benchmark is flawed; just use a Vec (as described here), possibly with into_boxed_slice, as it is incredibly unlikely to be slower than a heap allocated array. Unfortunately, your benchmarks are flawed. First of all, you probably didn’t compile with optimizations (–release for cargo, -O for rustc). Because if you would have, the Rust … Read more