本文简要介绍rust语言中 std::mem::MaybeUninit.write_slice
的用法。
用法
pub fn write_slice(this: &'a mut [MaybeUninit<T>], src: &[T]) -> &'a mut [T] where T: Copy,
将元素从 src
复制到 this
,返回对现在初始化的 this
内容的可变引用。
如果 T
未实现 Copy
,请使用 write_slice_cloned
这类似于 slice::copy_from_slice
。
Panics
如果两个切片的长度不同,此函数将出现Panics。
例子
#![feature(maybe_uninit_write_slice)]
use std::mem::MaybeUninit;
let mut dst = [MaybeUninit::uninit(); 32];
let src = [0; 32];
let init = MaybeUninit::write_slice(&mut dst, &src);
assert_eq!(init, src);
#![feature(maybe_uninit_write_slice, vec_spare_capacity)]
use std::mem::MaybeUninit;
let mut vec = Vec::with_capacity(32);
let src = [0; 16];
MaybeUninit::write_slice(&mut vec.spare_capacity_mut()[..src.len()], &src);
// SAFETY: we have just copied all the elements of len into the spare capacity
// the first src.len() elements of the vec are valid now.
unsafe {
vec.set_len(src.len());
}
assert_eq!(vec, src);
相关用法
- Rust MaybeUninit.write_slice_cloned用法及代码示例
- Rust MaybeUninit.write用法及代码示例
- Rust MaybeUninit.assume_init_mut用法及代码示例
- Rust MaybeUninit.as_ptr用法及代码示例
- Rust MaybeUninit.assume_init_ref用法及代码示例
- Rust MaybeUninit.zeroed用法及代码示例
- Rust MaybeUninit.assume_init用法及代码示例
- Rust MaybeUninit.as_mut_ptr用法及代码示例
- Rust MaybeUninit.array_assume_init用法及代码示例
- Rust MaybeUninit.uninit用法及代码示例
- Rust MaybeUninit.new用法及代码示例
- Rust MaybeUninit.uninit_array用法及代码示例
- Rust MaybeUninit.assume_init_read用法及代码示例
- Rust MaybeUninit用法及代码示例
- Rust ManuallyDrop用法及代码示例
- Rust ManuallyDrop.into_inner用法及代码示例
- Rust ManuallyDrop.new用法及代码示例
- Rust Map用法及代码示例
- Rust Mutex.new用法及代码示例
- Rust MetadataExt.st_ctime_nsec用法及代码示例
- Rust MetadataExt.mtime_nsec用法及代码示例
- Rust MetadataExt.nlink用法及代码示例
- Rust MulAssign.mul_assign用法及代码示例
- Rust Mutex.get_mut用法及代码示例
- Rust MetadataExt.st_atime用法及代码示例
注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 std::mem::MaybeUninit.write_slice。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。