diff options
| author | bors <bors@rust-lang.org> | 2017-09-10 12:48:55 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-09-10 12:48:55 +0000 |
| commit | d290dec97f8bace2a2585505518b109b1e368f4c (patch) | |
| tree | b3973604ffde6b7dce3897b531a7faaf8c0aeb69 /src/liballoc | |
| parent | 23aaeb573b626a51af9ecc97680663153e4ab2b0 (diff) | |
| parent | 8a7d93bf6f5b1a699bafec6f59964a3962b2e927 (diff) | |
| download | rust-d290dec97f8bace2a2585505518b109b1e368f4c.tar.gz rust-d290dec97f8bace2a2585505518b109b1e368f4c.zip | |
Auto merge of #44474 - GuillaumeGomez:rollup, r=GuillaumeGomez
Rollup of 13 pull requests - Successful merges: #44262, #44329, #44332, #44347, #44372, #44384, #44387, #44396, #44449, #44451, #44457, #44464, #44467 - Failed merges:
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/str.rs | 51 | ||||
| -rw-r--r-- | src/liballoc/string.rs | 10 |
2 files changed, 54 insertions, 7 deletions
diff --git a/src/liballoc/str.rs b/src/liballoc/str.rs index 79b2bbce2af..f0c63a2eb55 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] { @@ -362,16 +390,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] 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 { |
