Function core::ptr::slice_from_raw_parts_mut

1.42.0 (const: unstable) · source · []
pub fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T]
Expand description

Performs the same functionality as slice_from_raw_parts, except that a raw mutable slice is returned, as opposed to a raw immutable slice.

See the documentation of slice_from_raw_parts for more details.

This function is safe, but actually using the return value is unsafe. See the documentation of slice::from_raw_parts_mut for slice safety requirements.


use std::ptr;

let x = &mut [5, 6, 7];
let raw_pointer = x.as_mut_ptr();
let slice = ptr::slice_from_raw_parts_mut(raw_pointer, 3);

unsafe {
    (*slice)[2] = 99; // assign a value at an index in the slice

assert_eq!(unsafe { &*slice }[2], 99);