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