diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 386c7c2612d57..3cf6c56f43a57 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -2107,7 +2107,8 @@ impl VecDeque { /// assert_eq!(buf, [2, 4]); /// ``` /// - /// The exact order may be useful for tracking external state, like an index. + /// Because the elements are visited exactly once in the original order, + /// external state may be used to decide which elements to keep. /// /// ``` /// use std::collections::VecDeque; @@ -2116,8 +2117,8 @@ impl VecDeque { /// buf.extend(1..6); /// /// let keep = [false, true, true, false, true]; - /// let mut i = 0; - /// buf.retain(|_| (keep[i], i += 1).0); + /// let mut iter = keep.iter(); + /// buf.retain(|_| *iter.next().unwrap()); /// assert_eq!(buf, [2, 3, 5]); /// ``` #[stable(feature = "vec_deque_retain", since = "1.4.0")] diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index bc5ab3637ae6c..5411316f55af0 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -1350,13 +1350,14 @@ impl String { /// assert_eq!(s, "foobar"); /// ``` /// - /// The exact order may be useful for tracking external state, like an index. + /// Because the elements are visited exactly once in the original order, + /// external state may be used to decide which elements to keep. /// /// ``` /// let mut s = String::from("abcde"); /// let keep = [false, true, true, false, true]; - /// let mut i = 0; - /// s.retain(|_| (keep[i], i += 1).0); + /// let mut iter = keep.iter(); + /// s.retain(|_| *iter.next().unwrap()); /// assert_eq!(s, "bce"); /// ``` #[inline]