C++ ships with
std::fill which fills a
range of memory with a given byte. Rust currently doesn't have a standard
library equivalent for that.
Issue 2067 on the RFCs
repo proposes a safe wrapper around
memset(3) call. This is a reference
implementation of the API proposed in the issue that optimizes to
in many cases but leaves the exact performance profile undisclosed.
As Rust makes progress on specialization this can later be lifted to guarantee the fastest known implementation is used for any specific type. But until then it provides a convenient API to fill slices of memory with repeating patterns, which will generally be optimized to be very fast already.
use slice_fill::SliceExt; let mut buf = vec![0; 10]; buf.fill(1); assert_eq!(buf, vec![1; 10]);
Extension trait for