std::slice::from_raw_parts - Rust
Description: API documentation for the Rust `from_raw_parts` fn in crate `std`.
Keywords: rust, rustlang, rust-lang, from_raw_parts

# 1.0.0[−][src]Function std::slice::from_raw_parts

ⓘImportant traits for &'_ [u8]

### Important traits for &'_ [u8]

`impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [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.

# Safety

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 total size of the slice must be no larger than `isize::MAX` bytes in memory. See the safety documentation of `pointer::offset`.

# Caveat

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.

# Examples

    use std::slice;

    // manifest a slice for a single element
    let x = 42;
    let ptr = &x as *const _;
    let slice = unsafe { slice::from_raw_parts(ptr, 1) };
    assert_eq!(slice[0], 42);Run

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)