Add examples of ordered retain

This commit is contained in:
Josh Stone
2019-05-10 18:01:50 -07:00
parent 9b3583375d
commit 0545375ca6
3 changed files with 34 additions and 0 deletions
+10
View File
@@ -1212,6 +1212,16 @@ pub fn remove(&mut self, idx: usize) -> char {
///
/// assert_eq!(s, "foobar");
/// ```
///
/// The exact order may be useful for tracking external state, like an index.
///
/// ```
/// let mut s = String::from("abcde");
/// let keep = [false, true, true, false, true];
/// let mut i = 0;
/// s.retain(|_| (keep[i], i += 1).0);
/// assert_eq!(s, "bce");
/// ```
#[inline]
#[stable(feature = "string_retain", since = "1.26.0")]
pub fn retain<F>(&mut self, mut f: F)