From 3e8fadc2ac5d5e25aaf0449d50988c54be92dbe4 Mon Sep 17 00:00:00 2001 From: Tommy Ip Date: Sat, 9 Sep 2017 09:05:54 +0100 Subject: Add doc example to String::as_str Fixes #44428. --- src/liballoc/string.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/liballoc') diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs index ddb23b2ef37..1708f3e3987 100644 --- a/src/liballoc/string.rs +++ b/src/liballoc/string.rs @@ -743,6 +743,16 @@ impl String { } /// Extracts a string slice containing the entire string. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// let s = String::from("foo"); + /// + /// assert_eq!("foo", s.as_str()); + /// ``` #[inline] #[stable(feature = "string_as_str", since = "1.7.0")] pub fn as_str(&self) -> &str { -- cgit 1.4.1-3-g733a5 From 6c8993532c7799c745247fb7dc6293156e5c5b05 Mon Sep 17 00:00:00 2001 From: Ethan Dagner Date: Sat, 9 Sep 2017 10:27:47 -0600 Subject: Add doc examples for str::as_bytes_mut Fixes #44427 --- src/liballoc/str.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/liballoc') 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] { -- cgit 1.4.1-3-g733a5 From c430fa8084277c9ee36b022dd028ed302b3f5afe Mon Sep 17 00:00:00 2001 From: toidiu Date: Sat, 9 Sep 2017 16:56:12 -0400 Subject: documentation update to demonstrate mutability --- src/liballoc/str.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/str.rs b/src/liballoc/str.rs index 79b2bbce2af..e6b28d4d1c2 100644 --- a/src/liballoc/str.rs +++ b/src/liballoc/str.rs @@ -362,16 +362,25 @@ impl str { /// # Examples /// /// ``` - /// let mut v = String::from("🗻∈🌏"); - /// - /// assert_eq!(Some("🗻"), v.get_mut(0..4).map(|v| &*v)); - /// - /// // indices not on UTF-8 sequence boundaries - /// assert!(v.get_mut(1..).is_none()); - /// assert!(v.get_mut(..8).is_none()); + /// use std::ascii::AsciiExt; /// + /// let mut v = String::from("hello"); + /// // correct length + /// assert!(v.get_mut(0..5).is_some()); /// // out of bounds /// assert!(v.get_mut(..42).is_none()); + /// assert_eq!(Some("he"), v.get_mut(0..2).map(|v| &*v)); + /// + /// assert_eq!("hello", v); + /// { + /// let s = v.get_mut(0..2); + /// let s = s.map(|s| { + /// s.make_ascii_uppercase(); + /// &*s + /// }); + /// assert_eq!(Some("HE"), s); + /// } + /// assert_eq!("HEllo", v); /// ``` #[stable(feature = "str_checked_slicing", since = "1.20.0")] #[inline] -- cgit 1.4.1-3-g733a5