Function std::slice::from_raw_parts1.0.0[][src]

Important traits for &'a [u8]
pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T]

Forms a slice from a pointer and a length.

The len argument is the number of elements, not the number of bytes.


This function is unsafe as there is no guarantee that the given pointer is valid for len elements, nor whether the lifetime inferred is a suitable lifetime for the returned slice.

data must be non-null and aligned, even for zero-length slices. One reason for this is that enum layout optimizations may rely on references (including slices of any length) being aligned and non-null to distinguish them from other data. You can obtain a pointer that is usable as data for zero-length slices using NonNull::dangling().


The lifetime for the returned slice is inferred from its usage. To prevent accidental misuse, it's suggested to tie the lifetime to whichever source lifetime is safe in the context, such as by providing a helper function taking the lifetime of a host value for the slice, or by explicit annotation.


use std::slice;

// manifest a slice out of thin air!
let ptr = 0x1234 as *const usize;
let amt = 10;
unsafe {
    let slice = slice::from_raw_parts(ptr, amt);