about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorEthan Dagner <napen123@gmail.com>2017-09-09 10:27:47 -0600
committerEthan Dagner <napen123@gmail.com>2017-09-09 10:27:47 -0600
commit6c8993532c7799c745247fb7dc6293156e5c5b05 (patch)
tree9d26e58c01016f02764c921a2aac9f2b843ec2c7 /src/liballoc
parent18366f4e8aaec1d46282cf0a6e0fe1a0ab202530 (diff)
downloadrust-6c8993532c7799c745247fb7dc6293156e5c5b05.tar.gz
rust-6c8993532c7799c745247fb7dc6293156e5c5b05.zip
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] {