From bc1a4c635703e08f0ee5830b389b2b804e82d76b Mon Sep 17 00:00:00 2001 From: Tommy Ip Date: Sat, 9 Sep 2017 13:08:26 +0100 Subject: Add doc example to String::as_mut_str Fixes #44429. --- src/liballoc/string.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'src/liballoc') diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs index ddb23b2ef37..9fef66f2c0a 100644 --- a/src/liballoc/string.rs +++ b/src/liballoc/string.rs @@ -749,7 +749,22 @@ impl String { self } - /// Extracts a string slice containing the entire string. + /// Converts a `String` into a mutable string slice. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// use std::ascii::AsciiExt; + /// + /// let mut s = String::from("foobar"); + /// let s_mut_str = s.as_mut_str(); + /// + /// s_mut_str.make_ascii_uppercase(); + /// + /// assert_eq!("FOOBAR", s_mut_str); + /// ``` #[inline] #[stable(feature = "string_as_str", since = "1.7.0")] pub fn as_mut_str(&mut self) -> &mut str { -- cgit 1.4.1-3-g733a5 From 833a9b567ae15d691455457a476480d422e6f7a7 Mon Sep 17 00:00:00 2001 From: 42triangles Date: Mon, 11 Sep 2017 08:13:57 +0200 Subject: Added an example for `std::str::into_boxed_bytes()` --- src/liballoc/str.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/liballoc') diff --git a/src/liballoc/str.rs b/src/liballoc/str.rs index f0c63a2eb55..c48ff3f5cc3 100644 --- a/src/liballoc/str.rs +++ b/src/liballoc/str.rs @@ -1783,6 +1783,17 @@ impl str { } /// Converts a `Box` into a `Box<[u8]>` without copying or allocating. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// let s = "this is a string"; + /// let boxed_str = s.to_owned().into_boxed_str(); + /// let boxed_bytes = boxed_str.into_boxed_bytes(); + /// assert_eq!(*boxed_bytes, *s.as_bytes()); + /// ``` #[stable(feature = "str_box_extras", since = "1.20.0")] pub fn into_boxed_bytes(self: Box) -> Box<[u8]> { self.into() -- cgit 1.4.1-3-g733a5 From f452acbb5e18ae188bc28dc8ea685f6b4cb0d2df Mon Sep 17 00:00:00 2001 From: 42triangles Date: Mon, 11 Sep 2017 08:28:14 +0200 Subject: Removed trailing whitespace --- src/liballoc/str.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/liballoc') diff --git a/src/liballoc/str.rs b/src/liballoc/str.rs index c48ff3f5cc3..d4dcf2066df 100644 --- a/src/liballoc/str.rs +++ b/src/liballoc/str.rs @@ -1783,7 +1783,7 @@ impl str { } /// Converts a `Box` into a `Box<[u8]>` without copying or allocating. - /// + /// /// # Examples /// /// Basic usage: -- cgit 1.4.1-3-g733a5 From ede6dfd72a601a1da403d10849f56791d2144b40 Mon Sep 17 00:00:00 2001 From: Tommy Ip Date: Mon, 11 Sep 2017 17:28:28 +0100 Subject: Add doc example to str::from_boxed_utf8_unchecked Fixes #44463. --- src/liballoc/str.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/liballoc') diff --git a/src/liballoc/str.rs b/src/liballoc/str.rs index f0c63a2eb55..48b4d52a06a 100644 --- a/src/liballoc/str.rs +++ b/src/liballoc/str.rs @@ -2050,6 +2050,17 @@ impl str { /// Converts a boxed slice of bytes to a boxed string slice without checking /// that the string contains valid UTF-8. +/// +/// # Examples +/// +/// Basic usage: +/// +/// ``` +/// let smile_utf8 = Box::new([226, 152, 186]); +/// let smile = unsafe { std::str::from_boxed_utf8_unchecked(smile_utf8) }; +/// +/// assert_eq!("☺", &*smile); +/// ``` #[stable(feature = "str_box_extras", since = "1.20.0")] pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box { mem::transmute(v) -- cgit 1.4.1-3-g733a5 From b8e0989445b3e29affa529d8f1b8574267fda495 Mon Sep 17 00:00:00 2001 From: rwakulszowa Date: Tue, 12 Sep 2017 11:53:16 +0100 Subject: Add an example of std::str::encode_utf16 Closes #44419 --- src/liballoc/str.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/liballoc') diff --git a/src/liballoc/str.rs b/src/liballoc/str.rs index f0c63a2eb55..a665480bba7 100644 --- a/src/liballoc/str.rs +++ b/src/liballoc/str.rs @@ -855,6 +855,19 @@ impl str { } /// Returns an iterator of `u16` over the string encoded as UTF-16. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// let text = "Zażółć gęślą jaźń"; + /// + /// let utf8_len = text.len(); + /// let utf16_len = text.encode_utf16().count(); + /// + /// assert!(utf16_len <= utf8_len); + /// ``` #[stable(feature = "encode_utf16", since = "1.8.0")] pub fn encode_utf16(&self) -> EncodeUtf16 { EncodeUtf16 { encoder: Utf16Encoder::new(self[..].chars()) } -- cgit 1.4.1-3-g733a5 From 518bd3085413670d3f7ccbc77abb64a0cb4a198a Mon Sep 17 00:00:00 2001 From: David Adler Date: Tue, 12 Sep 2017 22:33:27 -0700 Subject: Fix drain_filter doctest. --- src/liballoc/vec.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 8141851b8c9..caca4010156 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -1969,16 +1969,19 @@ impl Vec { /// Using this method is equivalent to the following code: /// /// ``` - /// # let some_predicate = |x: &mut i32| { *x == 2 }; - /// # let mut vec = vec![1, 2, 3, 4, 5]; + /// # let some_predicate = |x: &mut i32| { *x == 2 || *x == 3 || *x == 6 }; + /// # let mut vec = vec![1, 2, 3, 4, 5, 6]; /// let mut i = 0; /// while i != vec.len() { /// if some_predicate(&mut vec[i]) { /// let val = vec.remove(i); /// // your code here + /// } else { + /// i += 1; /// } - /// i += 1; /// } + /// + /// # assert_eq!(vec, vec![1, 4, 5]); /// ``` /// /// But `drain_filter` is easier to use. `drain_filter` is also more efficient, -- cgit 1.4.1-3-g733a5 From 9dd2ee1942b232246bd87805c2be471af9fad20a Mon Sep 17 00:00:00 2001 From: Havvy Date: Wed, 13 Sep 2017 01:27:41 -0700 Subject: Fix example in transmute; add safety requirement to Vec::from_raw_parts --- src/liballoc/vec.rs | 1 + src/libcore/intrinsics.rs | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 8141851b8c9..87b2b3f7a40 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -370,6 +370,7 @@ impl Vec { /// /// * `ptr` needs to have been previously allocated via [`String`]/`Vec` /// (at least, it's highly likely to be incorrect if it wasn't). + /// * `ptr`'s `T` needs to have the same size and alignment as it was allocated with. /// * `length` needs to be less than or equal to `capacity`. /// * `capacity` needs to be the capacity that the pointer was allocated with. /// diff --git a/src/libcore/intrinsics.rs b/src/libcore/intrinsics.rs index 607f6f37017..f7f1dd12d28 100644 --- a/src/libcore/intrinsics.rs +++ b/src/libcore/intrinsics.rs @@ -848,12 +848,12 @@ extern "rust-intrinsic" { /// // The no-copy, unsafe way, still using transmute, but not UB. /// // This is equivalent to the original, but safer, and reuses the /// // same Vec internals. Therefore the new inner type must have the - /// // exact same size, and the same or lesser alignment, as the old - /// // type. The same caveats exist for this method as transmute, for + /// // exact same size, and the same alignment, as the old type. + /// // The same caveats exist for this method as transmute, for /// // the original inner type (`&i32`) to the converted inner type /// // (`Option<&i32>`), so read the nomicon pages linked above. /// let v_from_raw = unsafe { - /// Vec::from_raw_parts(v_orig.as_mut_ptr(), + /// Vec::from_raw_parts(v_orig.as_mut_ptr() as *mut Option<&i32>, /// v_orig.len(), /// v_orig.capacity()) /// }; -- cgit 1.4.1-3-g733a5 From 2d292cff5d0a1f8e3273879477553bb8717b6f78 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 13 Sep 2017 22:44:14 -0400 Subject: Remove unneeded `loop`. --- src/liballoc/btree/set.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/btree/set.rs b/src/liballoc/btree/set.rs index d32460da939..7da6371cc19 100644 --- a/src/liballoc/btree/set.rs +++ b/src/liballoc/btree/set.rs @@ -1110,15 +1110,13 @@ impl<'a, T: Ord> Iterator for Union<'a, T> { type Item = &'a T; fn next(&mut self) -> Option<&'a T> { - loop { - match cmp_opt(self.a.peek(), self.b.peek(), Greater, Less) { - Less => return self.a.next(), - Equal => { - self.b.next(); - return self.a.next(); - } - Greater => return self.b.next(), + match cmp_opt(self.a.peek(), self.b.peek(), Greater, Less) { + Less => self.a.next(), + Equal => { + self.b.next(); + self.a.next() } + Greater => self.b.next(), } } -- cgit 1.4.1-3-g733a5 From 258ef37f8e81388a11f420411e35b427215b0754 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Thu, 14 Sep 2017 15:34:31 -0400 Subject: Clarify return type of `String::from_utf16_lossy`. Fixes https://github.com/rust-lang/rust/issues/32874 --- src/liballoc/string.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/liballoc') diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs index 1708f3e3987..088082c261a 100644 --- a/src/liballoc/string.rs +++ b/src/liballoc/string.rs @@ -622,6 +622,13 @@ impl String { /// Decode a UTF-16 encoded slice `v` into a `String`, replacing /// invalid data with the replacement character (U+FFFD). /// + /// Unlike [`from_utf8_lossy`] which returns a [`Cow<'a, str>`], + /// `from_utf16_lossy` returns a `String` since the UTF-16 to UTF-8 + /// conversion requires a memory allocation. + /// + /// [`from_utf8_lossy`]: #method.from_utf8_lossy + /// [`Cow<'a, str>`]: ../borrow/enum.Cow.html + /// /// # Examples /// /// Basic usage: -- cgit 1.4.1-3-g733a5