Skip to content

Commit 3fca886

Browse files
committed
rollup merge of #24541: alexcrichton/issue-24538
This is an implementation of [RFC 1030][rfc] which adds these traits to the prelude and additionally removes all inherent `into_iter` methods on collections in favor of the trait implementation (which is now accessible by default). [rfc]: rust-lang/rfcs#1030 This is technically a breaking change due to the prelude additions and removal of inherent methods, but it is expected that essentially no code breaks in practice. [breaking-change] Closes #24538
2 parents acc06e7 + a0745a1 commit 3fca886

File tree

2 files changed

+47
-57
lines changed

2 files changed

+47
-57
lines changed

map.rs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -914,33 +914,6 @@ impl<K, V, S> HashMap<K, V, S>
914914
IterMut { inner: self.table.iter_mut() }
915915
}
916916

917-
/// Creates a consuming iterator, that is, one that moves each key-value
918-
/// pair out of the map in arbitrary order. The map cannot be used after
919-
/// calling this.
920-
///
921-
/// # Examples
922-
///
923-
/// ```
924-
/// use std::collections::HashMap;
925-
///
926-
/// let mut map = HashMap::new();
927-
/// map.insert("a", 1);
928-
/// map.insert("b", 2);
929-
/// map.insert("c", 3);
930-
///
931-
/// // Not possible with .iter()
932-
/// let vec: Vec<(&str, isize)> = map.into_iter().collect();
933-
/// ```
934-
#[stable(feature = "rust1", since = "1.0.0")]
935-
pub fn into_iter(self) -> IntoIter<K, V> {
936-
fn last_two<A, B, C>((_, b, c): (A, B, C)) -> (B, C) { (b, c) }
937-
let last_two: fn((SafeHash, K, V)) -> (K, V) = last_two;
938-
939-
IntoIter {
940-
inner: self.table.into_iter().map(last_two)
941-
}
942-
}
943-
944917
/// Gets the given key's corresponding entry in the map for in-place manipulation.
945918
#[stable(feature = "rust1", since = "1.0.0")]
946919
pub fn entry(&mut self, key: K) -> Entry<K, V> {
@@ -1388,8 +1361,30 @@ impl<K, V, S> IntoIterator for HashMap<K, V, S>
13881361
type Item = (K, V);
13891362
type IntoIter = IntoIter<K, V>;
13901363

1364+
/// Creates a consuming iterator, that is, one that moves each key-value
1365+
/// pair out of the map in arbitrary order. The map cannot be used after
1366+
/// calling this.
1367+
///
1368+
/// # Examples
1369+
///
1370+
/// ```
1371+
/// use std::collections::HashMap;
1372+
///
1373+
/// let mut map = HashMap::new();
1374+
/// map.insert("a", 1);
1375+
/// map.insert("b", 2);
1376+
/// map.insert("c", 3);
1377+
///
1378+
/// // Not possible with .iter()
1379+
/// let vec: Vec<(&str, isize)> = map.into_iter().collect();
1380+
/// ```
13911381
fn into_iter(self) -> IntoIter<K, V> {
1392-
self.into_iter()
1382+
fn last_two<A, B, C>((_, b, c): (A, B, C)) -> (B, C) { (b, c) }
1383+
let last_two: fn((SafeHash, K, V)) -> (K, V) = last_two;
1384+
1385+
IntoIter {
1386+
inner: self.table.into_iter().map(last_two)
1387+
}
13931388
}
13941389
}
13951390

set.rs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -269,34 +269,6 @@ impl<T, S> HashSet<T, S>
269269
Iter { iter: self.map.keys() }
270270
}
271271

272-
/// Creates a consuming iterator, that is, one that moves each value out
273-
/// of the set in arbitrary order. The set cannot be used after calling
274-
/// this.
275-
///
276-
/// # Examples
277-
///
278-
/// ```
279-
/// use std::collections::HashSet;
280-
/// let mut set = HashSet::new();
281-
/// set.insert("a".to_string());
282-
/// set.insert("b".to_string());
283-
///
284-
/// // Not possible to collect to a Vec<String> with a regular `.iter()`.
285-
/// let v: Vec<String> = set.into_iter().collect();
286-
///
287-
/// // Will print in an arbitrary order.
288-
/// for x in v.iter() {
289-
/// println!("{}", x);
290-
/// }
291-
/// ```
292-
#[stable(feature = "rust1", since = "1.0.0")]
293-
pub fn into_iter(self) -> IntoIter<T> {
294-
fn first<A, B>((a, _): (A, B)) -> A { a }
295-
let first: fn((T, ())) -> T = first;
296-
297-
IntoIter { iter: self.map.into_iter().map(first) }
298-
}
299-
300272
/// Visit the values representing the difference.
301273
///
302274
/// # Examples
@@ -848,8 +820,31 @@ impl<T, S> IntoIterator for HashSet<T, S>
848820
type Item = T;
849821
type IntoIter = IntoIter<T>;
850822

823+
/// Creates a consuming iterator, that is, one that moves each value out
824+
/// of the set in arbitrary order. The set cannot be used after calling
825+
/// this.
826+
///
827+
/// # Examples
828+
///
829+
/// ```
830+
/// use std::collections::HashSet;
831+
/// let mut set = HashSet::new();
832+
/// set.insert("a".to_string());
833+
/// set.insert("b".to_string());
834+
///
835+
/// // Not possible to collect to a Vec<String> with a regular `.iter()`.
836+
/// let v: Vec<String> = set.into_iter().collect();
837+
///
838+
/// // Will print in an arbitrary order.
839+
/// for x in v.iter() {
840+
/// println!("{}", x);
841+
/// }
842+
/// ```
851843
fn into_iter(self) -> IntoIter<T> {
852-
self.into_iter()
844+
fn first<A, B>((a, _): (A, B)) -> A { a }
845+
let first: fn((T, ())) -> T = first;
846+
847+
IntoIter { iter: self.map.into_iter().map(first) }
853848
}
854849
}
855850

0 commit comments

Comments
 (0)