Skip to content

Commit

Permalink
Add doc examples to str::from_utf8_unchecked_mut
Browse files Browse the repository at this point in the history
Fixes #44461
  • Loading branch information
napen123 committed Sep 10, 2017
1 parent d290dec commit 18ef0de
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/libcore/str/mod.rs
Expand Up @@ -382,6 +382,34 @@ pub unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
/// See the immutable version, [`from_utf8_unchecked()`][fromutf8], for more information.
///
/// [fromutf8]: fn.from_utf8_unchecked.html
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use std::str;
///
/// let mut heart = vec![240, 159, 146, 150];
/// let heart = unsafe { str::from_utf8_unchecked_mut(&mut heart) };
///
/// assert_eq!("💖", heart);
/// ```
///
/// Invalid UTF-8:
///
/// ```
/// use std::str;
///
/// // Invalid bytes.
/// let mut bytes = vec![240, 40, 140, 188];
///
/// // Returns a str:
/// unsafe { str::from_utf8_unchecked_mut(&mut bytes) };
///
/// // from_utf8 returns an error instead:
/// assert!(str::from_utf8(&bytes).is_err());
/// ```
#[inline]
#[stable(feature = "str_mut_extras", since = "1.20.0")]
pub unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str {
Expand Down

0 comments on commit 18ef0de

Please sign in to comment.