From 18ef0de4bce349917b8e89d85e8f5ce9676b3a86 Mon Sep 17 00:00:00 2001 From: Ethan Dagner Date: Sun, 10 Sep 2017 12:27:57 -0600 Subject: [PATCH 1/2] Add doc examples to str::from_utf8_unchecked_mut Fixes #44461 --- src/libcore/str/mod.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 62e84c9ebd0..858ada07344 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -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 { From 8adf50d90ecd7f7524e745c5d64edd203ebc737f Mon Sep 17 00:00:00 2001 From: Ethan Dagner Date: Tue, 12 Sep 2017 17:30:44 -0600 Subject: [PATCH 2/2] Remove Invalid UTF-8 from str::from_utf8_unchecked_mut --- src/libcore/str/mod.rs | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 858ada07344..3122354c48c 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -395,21 +395,6 @@ pub unsafe fn from_utf8_unchecked(v: &[u8]) -> &str { /// /// 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 {