Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace iterator-based construction of collections by Into<T> #91861

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions library/alloc/src/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ impl<T: Ord> BinaryHeap<T> {
///
/// ```
/// use std::collections::BinaryHeap;
/// let mut heap = BinaryHeap::from(vec![1, 3]);
/// let mut heap = BinaryHeap::from([1, 3]);
///
/// assert_eq!(heap.pop(), Some(3));
/// assert_eq!(heap.pop(), Some(1));
Expand Down Expand Up @@ -506,7 +506,7 @@ impl<T: Ord> BinaryHeap<T> {
/// ```
/// use std::collections::BinaryHeap;
///
/// let mut heap = BinaryHeap::from(vec![1, 2, 4, 5, 7]);
/// let mut heap = BinaryHeap::from([1, 2, 4, 5, 7]);
/// heap.push(6);
/// heap.push(3);
///
Expand Down Expand Up @@ -725,11 +725,8 @@ impl<T: Ord> BinaryHeap<T> {
/// ```
/// use std::collections::BinaryHeap;
///
/// let v = vec![-10, 1, 2, 3, 3];
/// let mut a = BinaryHeap::from(v);
///
/// let v = vec![-20, 5, 43];
/// let mut b = BinaryHeap::from(v);
/// let mut a = BinaryHeap::from([-10, 1, 2, 3, 3]);
/// let mut b = BinaryHeap::from([-20, 5, 43]);
///
/// a.append(&mut b);
///
Expand Down Expand Up @@ -765,7 +762,7 @@ impl<T: Ord> BinaryHeap<T> {
/// #![feature(binary_heap_drain_sorted)]
/// use std::collections::BinaryHeap;
///
/// let mut heap = BinaryHeap::from(vec![1, 2, 3, 4, 5]);
/// let mut heap = BinaryHeap::from([1, 2, 3, 4, 5]);
/// assert_eq!(heap.len(), 5);
///
/// drop(heap.drain_sorted()); // removes all elements in heap order
Expand All @@ -790,7 +787,7 @@ impl<T: Ord> BinaryHeap<T> {
/// #![feature(binary_heap_retain)]
/// use std::collections::BinaryHeap;
///
/// let mut heap = BinaryHeap::from(vec![-10, -5, 1, 2, 4, 13]);
/// let mut heap = BinaryHeap::from([-10, -5, 1, 2, 4, 13]);
///
/// heap.retain(|x| x % 2 == 0); // only keep even numbers
///
Expand Down Expand Up @@ -826,7 +823,7 @@ impl<T> BinaryHeap<T> {
///
/// ```
/// use std::collections::BinaryHeap;
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4]);
/// let heap = BinaryHeap::from([1, 2, 3, 4]);
///
/// // Print 1, 2, 3, 4 in arbitrary order
/// for x in heap.iter() {
Expand All @@ -848,9 +845,9 @@ impl<T> BinaryHeap<T> {
/// ```
/// #![feature(binary_heap_into_iter_sorted)]
/// use std::collections::BinaryHeap;
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4, 5]);
/// let heap = BinaryHeap::from([1, 2, 3, 4, 5]);
///
/// assert_eq!(heap.into_iter_sorted().take(2).collect::<Vec<_>>(), vec![5, 4]);
/// assert_eq!(heap.into_iter_sorted().take(2).collect::<Vec<_>>(), [5, 4]);
/// ```
#[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
pub fn into_iter_sorted(self) -> IntoIterSorted<T> {
Expand Down Expand Up @@ -1086,7 +1083,7 @@ impl<T> BinaryHeap<T> {
/// use std::collections::BinaryHeap;
/// use std::io::{self, Write};
///
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4, 5, 6, 7]);
/// let heap = BinaryHeap::from([1, 2, 3, 4, 5, 6, 7]);
///
/// io::sink().write(heap.as_slice()).unwrap();
/// ```
Expand All @@ -1105,7 +1102,7 @@ impl<T> BinaryHeap<T> {
///
/// ```
/// use std::collections::BinaryHeap;
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4, 5, 6, 7]);
/// let heap = BinaryHeap::from([1, 2, 3, 4, 5, 6, 7]);
/// let vec = heap.into_vec();
///
/// // Will print in some order
Expand All @@ -1127,7 +1124,7 @@ impl<T> BinaryHeap<T> {
///
/// ```
/// use std::collections::BinaryHeap;
/// let heap = BinaryHeap::from(vec![1, 3]);
/// let heap = BinaryHeap::from([1, 3]);
///
/// assert_eq!(heap.len(), 2);
/// ```
Expand Down Expand Up @@ -1171,7 +1168,7 @@ impl<T> BinaryHeap<T> {
///
/// ```
/// use std::collections::BinaryHeap;
/// let mut heap = BinaryHeap::from(vec![1, 3]);
/// let mut heap = BinaryHeap::from([1, 3]);
///
/// assert!(!heap.is_empty());
///
Expand All @@ -1195,7 +1192,7 @@ impl<T> BinaryHeap<T> {
///
/// ```
/// use std::collections::BinaryHeap;
/// let mut heap = BinaryHeap::from(vec![1, 3]);
/// let mut heap = BinaryHeap::from([1, 3]);
///
/// assert!(!heap.is_empty());
///
Expand Down Expand Up @@ -1616,7 +1613,7 @@ impl<T> IntoIterator for BinaryHeap<T> {
///
/// ```
/// use std::collections::BinaryHeap;
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4]);
/// let heap = BinaryHeap::from([1, 2, 3, 4]);
///
/// // Print 1, 2, 3, 4 in arbitrary order
/// for x in heap.into_iter() {
Expand Down
12 changes: 5 additions & 7 deletions library/alloc/src/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,10 +1098,8 @@ impl<K, V> BTreeMap<K, V> {
/// ```
/// use std::collections::BTreeMap;
///
/// let mut map: BTreeMap<&str, i32> = ["Alice", "Bob", "Carol", "Cheryl"]
/// .iter()
/// .map(|&s| (s, 0))
/// .collect();
/// let mut map: BTreeMap<&str, i32> =
/// [("Alice", 0), ("Bob", 0), ("Carol", 0), ("Cheryl", 0)].into();
/// for (_, balance) in map.range_mut("B".."Cheryl") {
/// *balance += 100;
/// }
Expand Down Expand Up @@ -1135,7 +1133,7 @@ impl<K, V> BTreeMap<K, V> {
/// let mut count: BTreeMap<&str, usize> = BTreeMap::new();
///
/// // count the number of occurrences of letters in the vec
/// for x in vec!["a", "b", "a", "c", "a", "b"] {
/// for x in ["a", "b", "a", "c", "a", "b"] {
/// *count.entry(x).or_insert(0) += 1;
/// }
///
Expand Down Expand Up @@ -1235,8 +1233,8 @@ impl<K, V> BTreeMap<K, V> {
/// let mut map: BTreeMap<i32, i32> = (0..8).map(|x| (x, x)).collect();
/// let evens: BTreeMap<_, _> = map.drain_filter(|k, _v| k % 2 == 0).collect();
/// let odds = map;
/// assert_eq!(evens.keys().copied().collect::<Vec<_>>(), vec![0, 2, 4, 6]);
/// assert_eq!(odds.keys().copied().collect::<Vec<_>>(), vec![1, 3, 5, 7]);
/// assert_eq!(evens.keys().copied().collect::<Vec<_>>(), [0, 2, 4, 6]);
/// assert_eq!(odds.keys().copied().collect::<Vec<_>>(), [1, 3, 5, 7]);
/// ```
#[unstable(feature = "btree_drain_filter", issue = "70530")]
pub fn drain_filter<F>(&mut self, pred: F) -> DrainFilter<'_, K, V, F>
Expand Down
30 changes: 15 additions & 15 deletions library/alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// ```
/// use std::collections::VecDeque;
///
/// let mut buf: VecDeque<i32> = [1].into_iter().collect();
/// let mut buf: VecDeque<i32> = [1].into();
/// buf.reserve_exact(10);
/// assert!(buf.capacity() >= 11);
/// ```
Expand All @@ -692,7 +692,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// ```
/// use std::collections::VecDeque;
///
/// let mut buf: VecDeque<i32> = [1].into_iter().collect();
/// let mut buf: VecDeque<i32> = [1].into();
/// buf.reserve(10);
/// assert!(buf.capacity() >= 11);
/// ```
Expand Down Expand Up @@ -1153,7 +1153,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// ```
/// use std::collections::VecDeque;
///
/// let v: VecDeque<_> = [1, 2, 3].into_iter().collect();
/// let v: VecDeque<_> = [1, 2, 3].into();
/// let range = v.range(2..).copied().collect::<VecDeque<_>>();
/// assert_eq!(range, [3]);
///
Expand Down Expand Up @@ -1188,17 +1188,17 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// ```
/// use std::collections::VecDeque;
///
/// let mut v: VecDeque<_> = [1, 2, 3].into_iter().collect();
/// let mut v: VecDeque<_> = [1, 2, 3].into();
/// for v in v.range_mut(2..) {
/// *v *= 2;
/// }
/// assert_eq!(v, vec![1, 2, 6]);
/// assert_eq!(v, [1, 2, 6]);
///
/// // A full range covers all contents
/// for v in v.range_mut(..) {
/// *v *= 2;
/// }
/// assert_eq!(v, vec![2, 4, 12]);
/// assert_eq!(v, [2, 4, 12]);
/// ```
#[inline]
#[stable(feature = "deque_range", since = "1.51.0")]
Expand Down Expand Up @@ -1235,7 +1235,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// ```
/// use std::collections::VecDeque;
///
/// let mut v: VecDeque<_> = [1, 2, 3].into_iter().collect();
/// let mut v: VecDeque<_> = [1, 2, 3].into();
/// let drained = v.drain(2..).collect::<VecDeque<_>>();
/// assert_eq!(drained, [3]);
/// assert_eq!(v, [1, 2]);
Expand Down Expand Up @@ -2025,7 +2025,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// ```
/// use std::collections::VecDeque;
///
/// let mut buf: VecDeque<_> = [1, 2, 3].into_iter().collect();
/// let mut buf: VecDeque<_> = [1, 2, 3].into();
/// let buf2 = buf.split_off(1);
/// assert_eq!(buf, [1]);
/// assert_eq!(buf2, [2, 3]);
Expand Down Expand Up @@ -2091,8 +2091,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// ```
/// use std::collections::VecDeque;
///
/// let mut buf: VecDeque<_> = [1, 2].into_iter().collect();
/// let mut buf2: VecDeque<_> = [3, 4].into_iter().collect();
/// let mut buf: VecDeque<_> = [1, 2].into();
/// let mut buf2: VecDeque<_> = [3, 4].into();
/// buf.append(&mut buf2);
/// assert_eq!(buf, [1, 2, 3, 4]);
/// assert_eq!(buf2, []);
Expand Down Expand Up @@ -2547,7 +2547,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// ```
/// use std::collections::VecDeque;
///
/// let deque: VecDeque<_> = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
/// let deque: VecDeque<_> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
///
/// assert_eq!(deque.binary_search(&13), Ok(9));
/// assert_eq!(deque.binary_search(&4), Err(7));
Expand All @@ -2562,7 +2562,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// ```
/// use std::collections::VecDeque;
///
/// let mut deque: VecDeque<_> = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
/// let mut deque: VecDeque<_> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
/// let num = 42;
/// let idx = deque.binary_search(&num).unwrap_or_else(|x| x);
/// deque.insert(idx, num);
Expand Down Expand Up @@ -2605,7 +2605,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// ```
/// use std::collections::VecDeque;
///
/// let deque: VecDeque<_> = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
/// let deque: VecDeque<_> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
///
/// assert_eq!(deque.binary_search_by(|x| x.cmp(&13)), Ok(9));
/// assert_eq!(deque.binary_search_by(|x| x.cmp(&4)), Err(7));
Expand Down Expand Up @@ -2658,7 +2658,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// ```
/// use std::collections::VecDeque;
///
/// let deque: VecDeque<_> = vec![(0, 0), (2, 1), (4, 1), (5, 1),
/// let deque: VecDeque<_> = [(0, 0), (2, 1), (4, 1), (5, 1),
/// (3, 1), (1, 2), (2, 3), (4, 5), (5, 8), (3, 13),
/// (1, 21), (2, 34), (4, 55)].into();
///
Expand Down Expand Up @@ -2701,7 +2701,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// ```
/// use std::collections::VecDeque;
///
/// let deque: VecDeque<_> = vec![1, 2, 3, 3, 5, 6, 7].into();
/// let deque: VecDeque<_> = [1, 2, 3, 3, 5, 6, 7].into();
/// let i = deque.partition_point(|&x| x < 5);
///
/// assert_eq!(i, 4);
Expand Down