std-method

slice::copy_from_slice

Copy elements of a Copy type into a mutable slice of exactly the same length. Use this to overwrite a fixed-size buffer without changing its length. Expected stdout (not executed): [0, 7, 8, 0] Common mistake: Copying a two-element source into a three-element destination. Length mismatch panics.

Formal Syntax & Structure

rust syntaxrust
destination.copy_from_slice(source)

Working Example

Example Snippetrust
fn main() {    let mut target = [0_u8; 4];    target[1..3].copy_from_slice(&[7, 8]);    println!("{target:?}");}
Rust Reference