Skip to content

Commit 046e090

Browse files
authored
Rollup merge of rust-lang#148995 - RalfJung:extract_if, r=the8472
add must_use to extract_if methods Also, mention the `.for_each(drop)` pattern in the documentation. One can't always use `retain` with a negated predicate as that does not have a range argument. r? `@the8472`
2 parents 6e2dce8 + fc07052 commit 046e090

File tree

9 files changed

+22
-10
lines changed

9 files changed

+22
-10
lines changed

library/alloc/src/collections/btree/map.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,8 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
14341434
///
14351435
/// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
14361436
/// or the iteration short-circuits, then the remaining elements will be retained.
1437-
/// Use [`retain`] with a negated predicate if you do not need the returned iterator.
1437+
/// Use `extract_if().for_each(drop)` if you do not need the returned iterator,
1438+
/// or [`retain`] with a negated predicate if you also do not need to restrict the range.
14381439
///
14391440
/// [`retain`]: BTreeMap::retain
14401441
///
@@ -1945,7 +1946,8 @@ impl<K, V> Default for Values<'_, K, V> {
19451946

19461947
/// An iterator produced by calling `extract_if` on BTreeMap.
19471948
#[stable(feature = "btree_extract_if", since = "1.91.0")]
1948-
#[must_use = "iterators are lazy and do nothing unless consumed"]
1949+
#[must_use = "iterators are lazy and do nothing unless consumed; \
1950+
use `retain` or `extract_if().for_each(drop)` to remove and discard elements"]
19491951
pub struct ExtractIf<
19501952
'a,
19511953
K,

library/alloc/src/collections/btree/set.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,8 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
11891189
///
11901190
/// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
11911191
/// or the iteration short-circuits, then the remaining elements will be retained.
1192-
/// Use [`retain`] with a negated predicate if you do not need the returned iterator.
1192+
/// Use `extract_if().for_each(drop)` if you do not need the returned iterator,
1193+
/// or [`retain`] with a negated predicate if you also do not need to restrict the range.
11931194
///
11941195
/// [`retain`]: BTreeSet::retain
11951196
/// # Examples
@@ -1547,7 +1548,8 @@ impl<'a, T, A: Allocator + Clone> IntoIterator for &'a BTreeSet<T, A> {
15471548

15481549
/// An iterator produced by calling `extract_if` on BTreeSet.
15491550
#[stable(feature = "btree_extract_if", since = "1.91.0")]
1550-
#[must_use = "iterators are lazy and do nothing unless consumed"]
1551+
#[must_use = "iterators are lazy and do nothing unless consumed; \
1552+
use `retain` or `extract_if().for_each(drop)` to remove and discard elements"]
15511553
pub struct ExtractIf<
15521554
'a,
15531555
T,

library/alloc/src/collections/linked_list.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1943,7 +1943,8 @@ impl<'a, T, A: Allocator> CursorMut<'a, T, A> {
19431943

19441944
/// An iterator produced by calling `extract_if` on LinkedList.
19451945
#[stable(feature = "extract_if", since = "1.87.0")]
1946-
#[must_use = "iterators are lazy and do nothing unless consumed"]
1946+
#[must_use = "iterators are lazy and do nothing unless consumed; \
1947+
use `extract_if().for_each(drop)` to remove and discard elements"]
19471948
pub struct ExtractIf<
19481949
'a,
19491950
T: 'a,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use crate::alloc::{Allocator, Global};
2121
/// let iter: ExtractIf<'_, _, _> = v.extract_if(.., |x| *x % 2 == 0);
2222
/// ```
2323
#[unstable(feature = "vec_deque_extract_if", issue = "147750")]
24-
#[must_use = "iterators are lazy and do nothing unless consumed"]
24+
#[must_use = "iterators are lazy and do nothing unless consumed; \
25+
use `retain_mut` or `extract_if().for_each(drop)` to remove and discard elements"]
2526
pub struct ExtractIf<
2627
'a,
2728
T,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
676676
///
677677
/// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
678678
/// or the iteration short-circuits, then the remaining elements will be retained.
679-
/// Use [`retain_mut`] with a negated predicate if you do not need the returned iterator.
679+
/// Use `extract_if().for_each(drop)` if you do not need the returned iterator,
680+
/// or [`retain_mut`] with a negated predicate if you also do not need to restrict the range.
680681
///
681682
/// [`retain_mut`]: VecDeque::retain_mut
682683
///

library/alloc/src/vec/extract_if.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ use crate::alloc::{Allocator, Global};
1616
/// let iter: std::vec::ExtractIf<'_, _, _> = v.extract_if(.., |x| *x % 2 == 0);
1717
/// ```
1818
#[stable(feature = "extract_if", since = "1.87.0")]
19-
#[must_use = "iterators are lazy and do nothing unless consumed"]
19+
#[must_use = "iterators are lazy and do nothing unless consumed; \
20+
use `retain_mut` or `extract_if().for_each(drop)` to remove and discard elements"]
2021
pub struct ExtractIf<
2122
'a,
2223
T,

library/alloc/src/vec/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3933,7 +3933,8 @@ impl<T, A: Allocator> Vec<T, A> {
39333933
///
39343934
/// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
39353935
/// or the iteration short-circuits, then the remaining elements will be retained.
3936-
/// Use [`retain_mut`] with a negated predicate if you do not need the returned iterator.
3936+
/// Use `extract_if().for_each(drop)` if you do not need the returned iterator,
3937+
/// or [`retain_mut`] with a negated predicate if you also do not need to restrict the range.
39373938
///
39383939
/// [`retain_mut`]: Vec::retain_mut
39393940
///

library/std/src/collections/hash/map.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1685,7 +1685,8 @@ impl<'a, K, V> Drain<'a, K, V> {
16851685
/// let iter = map.extract_if(|_k, v| *v % 2 == 0);
16861686
/// ```
16871687
#[stable(feature = "hash_extract_if", since = "1.88.0")]
1688-
#[must_use = "iterators are lazy and do nothing unless consumed"]
1688+
#[must_use = "iterators are lazy and do nothing unless consumed; \
1689+
use `retain` to remove and discard elements"]
16891690
pub struct ExtractIf<'a, K, V, F> {
16901691
base: base::ExtractIf<'a, K, V, F>,
16911692
}

library/std/src/collections/hash/set.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,8 @@ pub struct Drain<'a, K: 'a> {
13911391
/// let mut extract_ifed = a.extract_if(|v| v % 2 == 0);
13921392
/// ```
13931393
#[stable(feature = "hash_extract_if", since = "1.88.0")]
1394+
#[must_use = "iterators are lazy and do nothing unless consumed; \
1395+
use `retain` to remove and discard elements"]
13941396
pub struct ExtractIf<'a, K, F> {
13951397
base: base::ExtractIf<'a, K, F>,
13961398
}

0 commit comments

Comments
 (0)