@@ -292,6 +292,69 @@ pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
292292 from_raw_parts_mut ( data. cast ( ) , len)
293293}
294294
295+ /// Forms a raw string slice from a pointer and a length.
296+ ///
297+ /// The `len` argument is the number of **bytes**, not the number of characters.
298+ ///
299+ /// This function is safe, but actually using the return value is unsafe.
300+ /// See the documentation of [`slice::from_raw_parts`] for slice safety requirements and [`str::from_utf8`] for string safety requirements.
301+ ///
302+ /// [`slice::from_raw_parts`]: crate::slice::from_raw_parts
303+ /// [`str::from_utf8`]: crate::str::from_utf8
304+ ///
305+ /// # Examples
306+ ///
307+ /// ```rust
308+ /// #![feature(str_from_raw_parts)]
309+ /// use std::ptr;
310+ ///
311+ /// // create a string slice pointer when starting out with a pointer to the first element
312+ /// let x = "abc";
313+ /// let raw_pointer = x.as_ptr();
314+ /// let str = ptr::str_from_raw_parts(raw_pointer, 3);
315+ /// assert_eq!(unsafe { &*str }, x);
316+ /// ```
317+ #[ inline]
318+ #[ unstable( feature = "str_from_raw_parts" , issue = "none" ) ]
319+ #[ rustc_const_unstable( feature = "const_str_from_raw_parts" , issue = "none" ) ]
320+ pub const fn str_from_raw_parts ( data : * const u8 , len : usize ) -> * const str {
321+ from_raw_parts ( data. cast ( ) , len)
322+ }
323+
324+ /// Performs the same functionality as [`str_from_raw_parts`], except that a
325+ /// raw mutable string slice is returned, as opposed to a raw immutable string slice.
326+ ///
327+ /// See the documentation of [`slice_from_raw_parts`] for more details.
328+ ///
329+ /// This function is safe, but actually using the return value is unsafe.
330+ /// See the documentation of [`slice::from_raw_parts_mut`] for slice safety requirements and [`str::from_utf8_mut`] for string safety requirements.
331+ ///
332+ /// [`slice::from_raw_parts_mut`]: crate::slice::from_raw_parts_mut
333+ /// [`str::from_utf8_mut`]: crate::str::from_utf8_mut
334+ ///
335+ /// # Examples
336+ ///
337+ /// ```rust
338+ /// #![feature(str_from_raw_parts)]
339+ /// use std::ptr;
340+ ///
341+ /// let mut x = [b'a', b'b', b'c'];
342+ /// let raw_pointer = x.as_mut_ptr();
343+ /// let str = ptr::str_from_raw_parts_mut(raw_pointer, 3);
344+ ///
345+ /// unsafe {
346+ /// (*(str as *mut [u8]))[2] = b'z'; // assign a value at an index in the string slice
347+ /// };
348+ ///
349+ /// assert_eq!(unsafe { &*str }, "abz");
350+ /// ```
351+ #[ inline]
352+ #[ unstable( feature = "str_from_raw_parts" , issue = "none" ) ]
353+ #[ rustc_const_unstable( feature = "const_str_from_raw_parts" , issue = "none" ) ]
354+ pub const fn str_from_raw_parts_mut ( data : * mut u8 , len : usize ) -> * mut str {
355+ from_raw_parts_mut ( data. cast ( ) , len)
356+ }
357+
295358/// Swaps the values at two mutable locations of the same type, without
296359/// deinitializing either.
297360///
0 commit comments