File tree 1 file changed +28
-0
lines changed
1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -382,6 +382,34 @@ pub unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
382
382
/// See the immutable version, [`from_utf8_unchecked()`][fromutf8], for more information.
383
383
///
384
384
/// [fromutf8]: fn.from_utf8_unchecked.html
385
+ ///
386
+ /// # Examples
387
+ ///
388
+ /// Basic usage:
389
+ ///
390
+ /// ```
391
+ /// use std::str;
392
+ ///
393
+ /// let mut heart = vec![240, 159, 146, 150];
394
+ /// let heart = unsafe { str::from_utf8_unchecked_mut(&mut heart) };
395
+ ///
396
+ /// assert_eq!("💖", heart);
397
+ /// ```
398
+ ///
399
+ /// Invalid UTF-8:
400
+ ///
401
+ /// ```
402
+ /// use std::str;
403
+ ///
404
+ /// // Invalid bytes.
405
+ /// let mut bytes = vec![240, 40, 140, 188];
406
+ ///
407
+ /// // Returns a str:
408
+ /// unsafe { str::from_utf8_unchecked_mut(&mut bytes) };
409
+ ///
410
+ /// // from_utf8 returns an error instead:
411
+ /// assert!(str::from_utf8(&bytes).is_err());
412
+ /// ```
385
413
#[ inline]
386
414
#[ stable( feature = "str_mut_extras" , since = "1.20.0" ) ]
387
415
pub unsafe fn from_utf8_unchecked_mut ( v : & mut [ u8 ] ) -> & mut str {
You can’t perform that action at this time.
0 commit comments