Skip to content

Commit d4a60ab

Browse files
committed
Update the examples in String and VecDeque::retain
The examples added in #60396 used a "clever" post-increment hack, unrelated to the actual point of the examples. That hack was found [confusing] in the users forum, and #81811 already changed the `Vec` example to use a more direct iterator. This commit changes `String` and `VecDeque` in the same way for consistency. [confusing]: https://users.rust-lang.org/t/help-understand-strange-expression/62858
1 parent b708886 commit d4a60ab

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

library/alloc/src/collections/vec_deque/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2107,7 +2107,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
21072107
/// assert_eq!(buf, [2, 4]);
21082108
/// ```
21092109
///
2110-
/// The exact order may be useful for tracking external state, like an index.
2110+
/// Because the elements are visited exactly once in the original order,
2111+
/// external state may be used to decide which elements to keep.
21112112
///
21122113
/// ```
21132114
/// use std::collections::VecDeque;
@@ -2116,8 +2117,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
21162117
/// buf.extend(1..6);
21172118
///
21182119
/// let keep = [false, true, true, false, true];
2119-
/// let mut i = 0;
2120-
/// buf.retain(|_| (keep[i], i += 1).0);
2120+
/// let mut iter = keep.iter();
2121+
/// buf.retain(|_| *iter.next().unwrap());
21212122
/// assert_eq!(buf, [2, 3, 5]);
21222123
/// ```
21232124
#[stable(feature = "vec_deque_retain", since = "1.4.0")]

library/alloc/src/string.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1350,13 +1350,14 @@ impl String {
13501350
/// assert_eq!(s, "foobar");
13511351
/// ```
13521352
///
1353-
/// The exact order may be useful for tracking external state, like an index.
1353+
/// Because the elements are visited exactly once in the original order,
1354+
/// external state may be used to decide which elements to keep.
13541355
///
13551356
/// ```
13561357
/// let mut s = String::from("abcde");
13571358
/// let keep = [false, true, true, false, true];
1358-
/// let mut i = 0;
1359-
/// s.retain(|_| (keep[i], i += 1).0);
1359+
/// let mut iter = keep.iter();
1360+
/// s.retain(|_| *iter.next().unwrap());
13601361
/// assert_eq!(s, "bce");
13611362
/// ```
13621363
#[inline]

0 commit comments

Comments
 (0)