about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2017-09-10 14:03:30 +0200
committerGitHub <noreply@github.com>2017-09-10 14:03:30 +0200
commit329ed4affe07cb935dd3c28eade8673b53eeaae2 (patch)
treea57b8721fd69bede9e1bae7131659d5cf4a7636d /src/liballoc
parent485847dd701b5d2161fb8b13730d93e14d6bcfcc (diff)
parent6c8993532c7799c745247fb7dc6293156e5c5b05 (diff)
downloadrust-329ed4affe07cb935dd3c28eade8673b53eeaae2.tar.gz
rust-329ed4affe07cb935dd3c28eade8673b53eeaae2.zip
Rollup merge of #44457 - napen123:master, r=frewsxcv
Add doc examples for str::as_bytes_mut

Fixes #44427
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/str.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/liballoc/str.rs b/src/liballoc/str.rs
index 79b2bbce2af..c1252b219c4 100644
--- a/src/liballoc/str.rs
+++ b/src/liballoc/str.rs
@@ -297,6 +297,34 @@ impl str {
     /// [`str::from_utf8_mut`] function.
     ///
     /// [`str::from_utf8_mut`]: ./str/fn.from_utf8_mut.html
+    ///
+    /// # Examples
+    ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// let mut s = String::from("Hello");
+    /// let bytes = unsafe { s.as_bytes_mut() };
+    ///
+    /// assert_eq!(b"Hello", bytes);
+    /// ```
+    ///
+    /// Mutability:
+    ///
+    /// ```
+    /// let mut s = String::from("🗻∈🌏");
+    ///
+    /// unsafe {
+    ///     let bytes = s.as_bytes_mut();
+    ///
+    ///     bytes[0] = 0xF0;
+    ///     bytes[1] = 0x9F;
+    ///     bytes[2] = 0x8D;
+    ///     bytes[3] = 0x94;
+    /// }
+    ///
+    /// assert_eq!("🍔∈🌏", s);
+    /// ```
     #[stable(feature = "str_mut_extras", since = "1.20.0")]
     #[inline(always)]
     pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {