Skip to content

Commit a0745a1

Browse files
committed
std: Add Default/IntoIterator/ToOwned to the prelude
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
1 parent 26248c7 commit a0745a1

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
@@ -916,33 +916,6 @@ impl<K, V, S> HashMap<K, V, S>
916916
IterMut { inner: self.table.iter_mut() }
917917
}
918918

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

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

set.rs

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

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

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

0 commit comments

Comments
 (0)