Add examples of ordered retain
This commit is contained in:
parent
9b3583375d
commit
0545375ca6
3 changed files with 34 additions and 0 deletions
|
@ -1848,6 +1848,20 @@ impl<T> VecDeque<T> {
|
|||
/// buf.retain(|&x| x%2 == 0);
|
||||
/// assert_eq!(buf, [2, 4]);
|
||||
/// ```
|
||||
///
|
||||
/// The exact order may be useful for tracking external state, like an index.
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::VecDeque;
|
||||
///
|
||||
/// let mut buf = VecDeque::new();
|
||||
/// buf.extend(1..6);
|
||||
///
|
||||
/// let keep = [false, true, true, false, true];
|
||||
/// let mut i = 0;
|
||||
/// buf.retain(|_| (keep[i], i += 1).0);
|
||||
/// assert_eq!(buf, [2, 3, 5]);
|
||||
/// ```
|
||||
#[stable(feature = "vec_deque_retain", since = "1.4.0")]
|
||||
pub fn retain<F>(&mut self, mut f: F)
|
||||
where F: FnMut(&T) -> bool
|
||||
|
|
|
@ -1212,6 +1212,16 @@ impl String {
|
|||
///
|
||||
/// 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)
|
||||
|
|
|
@ -947,6 +947,16 @@ impl<T> Vec<T> {
|
|||
/// vec.retain(|&x| x%2 == 0);
|
||||
/// assert_eq!(vec, [2, 4]);
|
||||
/// ```
|
||||
///
|
||||
/// The exact order may be useful for tracking external state, like an index.
|
||||
///
|
||||
/// ```
|
||||
/// let mut vec = vec![1, 2, 3, 4, 5];
|
||||
/// let keep = [false, true, true, false, true];
|
||||
/// let mut i = 0;
|
||||
/// vec.retain(|_| (keep[i], i += 1).0);
|
||||
/// assert_eq!(vec, [2, 3, 5]);
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn retain<F>(&mut self, mut f: F)
|
||||
where F: FnMut(&T) -> bool
|
||||
|
|
Loading…
Reference in a new issue