Rollup merge of #91861 - juniorbassani:use-from-array-in-collections-examples, r=yaahc
Replace iterator-based construction of collections by `Into<T>` Just a few quality of life improvements in the doc examples. I also removed some `Vec`s in favor of arrays.
This commit is contained in:
commit
1dd0ac1f6a
@ -433,7 +433,7 @@ pub fn peek_mut(&mut self) -> Option<PeekMut<'_, 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));
|
||||
@ -506,7 +506,7 @@ pub fn push(&mut self, item: 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);
|
||||
///
|
||||
@ -725,11 +725,8 @@ fn rebuild(&mut self) {
|
||||
/// ```
|
||||
/// 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);
|
||||
///
|
||||
@ -765,7 +762,7 @@ pub fn append(&mut self, other: &mut Self) {
|
||||
/// #![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
|
||||
@ -790,7 +787,7 @@ pub fn drain_sorted(&mut self) -> DrainSorted<'_, 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
|
||||
///
|
||||
@ -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() {
|
||||
@ -848,9 +845,9 @@ pub fn iter(&self) -> Iter<'_, 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> {
|
||||
@ -1086,7 +1083,7 @@ pub fn shrink_to(&mut self, min_capacity: usize) {
|
||||
/// 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();
|
||||
/// ```
|
||||
@ -1105,7 +1102,7 @@ pub fn as_slice(&self) -> &[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
|
||||
@ -1127,7 +1124,7 @@ pub fn into_vec(self) -> Vec<T> {
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::BinaryHeap;
|
||||
/// let heap = BinaryHeap::from(vec![1, 3]);
|
||||
/// let heap = BinaryHeap::from([1, 3]);
|
||||
///
|
||||
/// assert_eq!(heap.len(), 2);
|
||||
/// ```
|
||||
@ -1171,7 +1168,7 @@ pub fn is_empty(&self) -> bool {
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::BinaryHeap;
|
||||
/// let mut heap = BinaryHeap::from(vec![1, 3]);
|
||||
/// let mut heap = BinaryHeap::from([1, 3]);
|
||||
///
|
||||
/// assert!(!heap.is_empty());
|
||||
///
|
||||
@ -1195,7 +1192,7 @@ pub fn drain(&mut self) -> Drain<'_, T> {
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::BinaryHeap;
|
||||
/// let mut heap = BinaryHeap::from(vec![1, 3]);
|
||||
/// let mut heap = BinaryHeap::from([1, 3]);
|
||||
///
|
||||
/// assert!(!heap.is_empty());
|
||||
///
|
||||
@ -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() {
|
||||
|
@ -1098,10 +1098,8 @@ pub fn range<T: ?Sized, R>(&self, range: R) -> Range<'_, 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;
|
||||
/// }
|
||||
@ -1135,7 +1133,7 @@ pub fn range_mut<T: ?Sized, R>(&mut self, range: R) -> RangeMut<'_, 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;
|
||||
/// }
|
||||
///
|
||||
@ -1235,8 +1233,8 @@ pub fn split_off<Q: ?Sized + Ord>(&mut self, key: &Q) -> Self
|
||||
/// 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>
|
||||
|
@ -669,7 +669,7 @@ pub fn capacity(&self) -> usize {
|
||||
/// ```
|
||||
/// 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);
|
||||
/// ```
|
||||
@ -692,7 +692,7 @@ pub fn reserve_exact(&mut self, additional: usize) {
|
||||
/// ```
|
||||
/// 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);
|
||||
/// ```
|
||||
@ -1153,7 +1153,7 @@ fn range_tail_head<R>(&self, range: R) -> (usize, usize)
|
||||
/// ```
|
||||
/// 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]);
|
||||
///
|
||||
@ -1188,17 +1188,17 @@ pub fn range<R>(&self, range: R) -> Iter<'_, T>
|
||||
/// ```
|
||||
/// 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")]
|
||||
@ -1235,7 +1235,7 @@ pub fn range_mut<R>(&mut self, range: R) -> IterMut<'_, T>
|
||||
/// ```
|
||||
/// 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]);
|
||||
@ -2025,7 +2025,7 @@ pub fn remove(&mut self, index: usize) -> Option<T> {
|
||||
/// ```
|
||||
/// 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]);
|
||||
@ -2091,8 +2091,8 @@ pub fn split_off(&mut self, at: usize) -> Self
|
||||
/// ```
|
||||
/// 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, []);
|
||||
@ -2547,7 +2547,7 @@ unsafe fn rotate_right_inner(&mut self, k: usize) {
|
||||
/// ```
|
||||
/// 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));
|
||||
@ -2562,7 +2562,7 @@ unsafe fn rotate_right_inner(&mut self, k: usize) {
|
||||
/// ```
|
||||
/// 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);
|
||||
@ -2605,7 +2605,7 @@ pub fn binary_search(&self, x: &T) -> Result<usize, usize>
|
||||
/// ```
|
||||
/// 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));
|
||||
@ -2658,7 +2658,7 @@ pub fn binary_search_by<'a, F>(&'a self, mut f: F) -> Result<usize, usize>
|
||||
/// ```
|
||||
/// 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();
|
||||
///
|
||||
@ -2701,7 +2701,7 @@ pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result<usize
|
||||
/// ```
|
||||
/// 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);
|
||||
|
Loading…
Reference in New Issue
Block a user