collections::slice::from_raw_parts

Function collections::slice::from_raw_parts

pub unsafe fn from_raw_parts<'a, T>(p: *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.

p must be non-null, even for zero-length slices.

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 out of thin air!
let ptr = 0x1234 as *const usize;
let amt = 10;
unsafe {
    let slice = slice::from_raw_parts(ptr, amt);
}

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/collections/slice/fn.from_raw_parts.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部