Function core::ptr::swap_nonoverlapping 1.27.0 (const: unstable)[−][src]
pub unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize)Expand description
从 x 和 y 开始在两个内存区域之间交换 count * size_of::<T>() 字节。
这两个区域必须 不能 重叠。
Safety
如果违反以下任一条件,则行为是未定义的:
-
x和y都必须 有效 才能读取和写入count * size_of::<T>()个字节。 -
x和y必须正确对齐。 -
从
x开始的内存区域,大小为count * size_of::<T>()字节不得与以y开始且大小相同的内存区域重叠。
请注意,即使有效复制的大小 (count * size_of::<T>()) 是 0,指针也必须非空的并且正确对齐。
Examples
基本用法:
use std::ptr;
let mut x = [1, 2, 3, 4];
let mut y = [7, 8, 9];
unsafe {
ptr::swap_nonoverlapping(x.as_mut_ptr(), y.as_mut_ptr(), 2);
}
assert_eq!(x, [7, 8, 3, 4]);
assert_eq!(y, [1, 2, 9]);Run