本文簡要介紹rust語言中 std::mem::MaybeUninit.write_slice_cloned
的用法。
用法
pub fn write_slice_cloned( this: &'a mut [MaybeUninit<T>], src: &[T]) -> &'a mut [T] where T: Clone,
將元素從 src
克隆到 this
,返回對現在已初始化的 this
內容的可變引用。任何已經初始化的元素都不會被刪除。
如果 T
實現 Copy
,請使用 write_slice
這類似於 slice::clone_from_slice
,但不會刪除現有元素。
Panics
如果兩個切片的長度不同,或者 Clone
的實現出現Panics,此函數將出現Panics。
如果出現Panics,已經克隆的元素將被刪除。
例子
#![feature(maybe_uninit_write_slice)]
use std::mem::MaybeUninit;
let mut dst = [MaybeUninit::uninit(), MaybeUninit::uninit(), MaybeUninit::uninit(), MaybeUninit::uninit(), MaybeUninit::uninit()];
let src = ["wibbly".to_string(), "wobbly".to_string(), "timey".to_string(), "wimey".to_string(), "stuff".to_string()];
let init = MaybeUninit::write_slice_cloned(&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 = ["rust", "is", "a", "pretty", "cool", "language"];
MaybeUninit::write_slice_cloned(&mut vec.spare_capacity_mut()[..src.len()], &src);
// SAFETY: we have just cloned 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用法及代碼示例
- 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_cloned。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。