One line of code, guaranteed to be optimized, easy to teach because it has a name:
s.fill(x); // See the RFC regarding the new propose name.
Three lines of code, not guaranteed to be optimized, hard to teach because it doesn’t have a name:
for elt in &mut [....] {
*elt = x;
}
Note that the compiler doesn’t optimize this simpler variant of the above well at all:
for i in 0..s.len() {
s[i] = x;
}
In particular, every iteration has a bounds check in it.
The existence of copy_memory in unstable Rust currently, and the existence of memset and memcpy in C, and the existence of std::fill and std::copy in C++ are evidence that it is worth having a named function for these.
Note also that bluss has already showed that the compiler will optimize a for loop over two slices efficiently, if it formed in a particular way: https://users.rust-lang.org/t/how-to-zip-two-slices-efficiently/2048