std-function
Copy between disjoint memory ranges
This operation requires non-overlapping read and write spans and appropriate pointer alignment. It copies the initialization state, so copying uninitialized bytes does not make them initialized. Here separate live arrays of Copy values make the conditions straightforward. Expected stdout (not executed): [8, 9] Common mistake: Passing a byte count instead of an element count.
Formal Syntax & Structure
rust syntaxrust
unsafe { std::ptr::copy_nonoverlapping(src, dst, count) }Working Example
Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() { let source = [8_u16, 9]; let mut dest = [0_u16; 2]; // SAFETY: separate arrays, both aligned and valid for two u16 elements; Copy values. unsafe { std::ptr::copy_nonoverlapping(source.as_ptr(), dest.as_mut_ptr(), 2); } println!("{dest:?}");}