std-function
Construct a raw slice pointer with length metadata
A raw slice pointer carries element-count information as well as a data pointer. Constructing it is safe, but turning it into a reference requires a valid aligned, non-null memory range of initialized elements, fitting the applicable size bound, with correct aliasing and lifetime. A length supplied by the caller is not independently checked against storage. Expected stdout (not executed): 3:4 Common mistake: Choosing a larger length than the source allocation can support.
Formal Syntax & Structure
rust syntaxrust
std::ptr::slice_from_raw_parts(data, len)Working Example
Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() { let items = [2_u8, 4, 6]; let raw = std::ptr::slice_from_raw_parts(items.as_ptr(), items.len()); assert_eq!(raw.len(), 3); // SAFETY: data and length originate from the live initialized array; shared borrow only. let slice = unsafe { &*raw }; println!("{}:{}", slice.len(), slice[1]);}