本文簡要介紹rust語言中 std::mem::MaybeUninit.uninit_array
的用法。
用法
pub fn uninit_array<const LEN: usize>() -> [MaybeUninit<T>; LEN]
創建一個新的 MaybeUninit<T>
項目數組,處於未初始化狀態。
注意:在未來的 Rust 版本中,當 Rust 允許 inline const expressions 時,此方法可能變得不必要。下麵的示例可以使用 let mut buf = [const { MaybeUninit::<u8>::uninit() }; 32];
。
例子
#![feature(maybe_uninit_uninit_array, maybe_uninit_extra, maybe_uninit_slice)]
use std::mem::MaybeUninit;
extern "C" {
fn read_into_buffer(ptr: *mut u8, max_len: usize) -> usize;
}
/// Returns a (possibly smaller) slice of data that was actually read
fn read(buf: &mut [MaybeUninit<u8>]) -> &[u8] {
unsafe {
let len = read_into_buffer(buf.as_mut_ptr() as *mut u8, buf.len());
MaybeUninit::slice_assume_init_ref(&buf[..len])
}
}
let mut buf: [MaybeUninit<u8>; 32] = MaybeUninit::uninit_array();
let data = read(&mut buf);
相關用法
- Rust MaybeUninit.uninit用法及代碼示例
- Rust MaybeUninit.assume_init_mut用法及代碼示例
- Rust MaybeUninit.as_ptr用法及代碼示例
- Rust MaybeUninit.write_slice用法及代碼示例
- Rust MaybeUninit.assume_init_ref用法及代碼示例
- Rust MaybeUninit.zeroed用法及代碼示例
- Rust MaybeUninit.write_slice_cloned用法及代碼示例
- Rust MaybeUninit.assume_init用法及代碼示例
- Rust MaybeUninit.write用法及代碼示例
- Rust MaybeUninit.as_mut_ptr用法及代碼示例
- Rust MaybeUninit.array_assume_init用法及代碼示例
- Rust MaybeUninit.new用法及代碼示例
- 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.uninit_array。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。