本文簡要介紹rust語言中 std::ptr::NonNull.slice_from_raw_parts
的用法。
用法
pub fn slice_from_raw_parts(data: NonNull<T>, len: usize) -> NonNull<[T]>
從細指針和長度創建非空原始切片。
len
參數是數量元素,而不是字節數。
此函數是安全的,但取消引用返回值是不安全的。有關切片安全要求,請參閱 slice::from_raw_parts
的文檔。
例子
#![feature(nonnull_slice_from_raw_parts)]
use std::ptr::NonNull;
// create a slice pointer when starting out with a pointer to the first element
let mut x = [5, 6, 7];
let nonnull_pointer = NonNull::new(x.as_mut_ptr()).unwrap();
let slice = NonNull::slice_from_raw_parts(nonnull_pointer, 3);
assert_eq!(unsafe { slice.as_ref()[2] }, 7);
(請注意,此示例人為地演示了此方法的使用,但 let slice = NonNull::from(&x[..]);
將是編寫此類代碼的更好方法。)
相關用法
- Rust NonNull.len用法及代碼示例
- Rust NonNull.as_uninit_slice_mut用法及代碼示例
- Rust NonNull.as_mut_ptr用法及代碼示例
- Rust NonNull.as_mut用法及代碼示例
- Rust NonNull.cast用法及代碼示例
- Rust NonNull.as_ptr用法及代碼示例
- Rust NonNull.new_unchecked用法及代碼示例
- Rust NonNull.get_unchecked_mut用法及代碼示例
- Rust NonNull.new用法及代碼示例
- Rust NonNull.as_non_null_ptr用法及代碼示例
- Rust NonNull.dangling用法及代碼示例
- Rust NonNull.as_ref用法及代碼示例
- Rust NonZeroU128.checked_pow用法及代碼示例
- Rust NonZeroUsize.checked_next_power_of_two用法及代碼示例
- Rust NonZeroI128.saturating_pow用法及代碼示例
- Rust NonZeroI64.wrapping_abs用法及代碼示例
- Rust NonZeroU64.checked_mul用法及代碼示例
- Rust NonZeroU16.saturating_pow用法及代碼示例
- Rust NonZeroU8.checked_add用法及代碼示例
- Rust NonZeroUsize.checked_pow用法及代碼示例
- Rust NonZeroU8.unchecked_add用法及代碼示例
- Rust NonZeroUsize.leading_zeros用法及代碼示例
- Rust NonZeroI32.trailing_zeros用法及代碼示例
- Rust NonZeroI8.saturating_pow用法及代碼示例
- Rust NonZeroIsize.wrapping_abs用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 std::ptr::NonNull.slice_from_raw_parts。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。