std-method

slice::copy_within

Copy a range of Copy elements within one slice. The source and destination may overlap; the destination must fit inside the slice. Expected stdout (not executed): [1, 2, 1, 2, 3] Common mistake: Assuming copying within a slice inserts new elements or increases its length.

Formal Syntax & Structure

rust syntaxrust
slice.copy_within(source_range, destination_start)

Working Example

Example Snippetrust
fn main() {    let mut buffer = [1, 2, 3, 4, 5];    buffer.copy_within(0..3, 2);    println!("{buffer:?}");}
Rust Reference