mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Rollup merge of #91709 - juniorbassani:use-from-array-in-set-examples, r=jyn514
Replace iterator-based set construction by *Set::From<[T; N]> This uses the array-based construction for `BtreeSet`s and `HashSet`s instead of first creating an iterator. I could also replace the `let mut a = Set::new(); a.insert(...);` fragments if desired.
This commit is contained in:
commit
3beeb75dde
@ -491,7 +491,7 @@ impl<T> BTreeSet<T> {
|
||||
/// ```
|
||||
/// use std::collections::BTreeSet;
|
||||
///
|
||||
/// let set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
|
||||
/// let set = BTreeSet::from([1, 2, 3]);
|
||||
/// assert_eq!(set.contains(&1), true);
|
||||
/// assert_eq!(set.contains(&4), false);
|
||||
/// ```
|
||||
@ -515,7 +515,7 @@ impl<T> BTreeSet<T> {
|
||||
/// ```
|
||||
/// use std::collections::BTreeSet;
|
||||
///
|
||||
/// let set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
|
||||
/// let set = BTreeSet::from([1, 2, 3]);
|
||||
/// assert_eq!(set.get(&2), Some(&2));
|
||||
/// assert_eq!(set.get(&4), None);
|
||||
/// ```
|
||||
@ -536,7 +536,7 @@ impl<T> BTreeSet<T> {
|
||||
/// ```
|
||||
/// use std::collections::BTreeSet;
|
||||
///
|
||||
/// let a: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
|
||||
/// let a = BTreeSet::from([1, 2, 3]);
|
||||
/// let mut b = BTreeSet::new();
|
||||
///
|
||||
/// assert_eq!(a.is_disjoint(&b), true);
|
||||
@ -562,7 +562,7 @@ impl<T> BTreeSet<T> {
|
||||
/// ```
|
||||
/// use std::collections::BTreeSet;
|
||||
///
|
||||
/// let sup: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
|
||||
/// let sup = BTreeSet::from([1, 2, 3]);
|
||||
/// let mut set = BTreeSet::new();
|
||||
///
|
||||
/// assert_eq!(set.is_subset(&sup), true);
|
||||
@ -639,7 +639,7 @@ impl<T> BTreeSet<T> {
|
||||
/// ```
|
||||
/// use std::collections::BTreeSet;
|
||||
///
|
||||
/// let sub: BTreeSet<_> = [1, 2].iter().cloned().collect();
|
||||
/// let sub = BTreeSet::from([1, 2]);
|
||||
/// let mut set = BTreeSet::new();
|
||||
///
|
||||
/// assert_eq!(set.is_superset(&sub), false);
|
||||
@ -853,7 +853,7 @@ impl<T> BTreeSet<T> {
|
||||
/// ```
|
||||
/// use std::collections::BTreeSet;
|
||||
///
|
||||
/// let mut set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
|
||||
/// let mut set = BTreeSet::from([1, 2, 3]);
|
||||
/// assert_eq!(set.take(&2), Some(2));
|
||||
/// assert_eq!(set.take(&2), None);
|
||||
/// ```
|
||||
@ -876,8 +876,7 @@ impl<T> BTreeSet<T> {
|
||||
/// ```
|
||||
/// use std::collections::BTreeSet;
|
||||
///
|
||||
/// let xs = [1, 2, 3, 4, 5, 6];
|
||||
/// let mut set: BTreeSet<i32> = xs.iter().cloned().collect();
|
||||
/// let mut set = BTreeSet::from([1, 2, 3, 4, 5, 6]);
|
||||
/// // Keep only the even numbers.
|
||||
/// set.retain(|&k| k % 2 == 0);
|
||||
/// assert!(set.iter().eq([2, 4, 6].iter()));
|
||||
@ -1009,7 +1008,7 @@ impl<T> BTreeSet<T> {
|
||||
/// ```
|
||||
/// use std::collections::BTreeSet;
|
||||
///
|
||||
/// let set: BTreeSet<usize> = [1, 2, 3].iter().cloned().collect();
|
||||
/// let set = BTreeSet::from([1, 2, 3]);
|
||||
/// let mut set_iter = set.iter();
|
||||
/// assert_eq!(set_iter.next(), Some(&1));
|
||||
/// assert_eq!(set_iter.next(), Some(&2));
|
||||
@ -1022,7 +1021,7 @@ impl<T> BTreeSet<T> {
|
||||
/// ```
|
||||
/// use std::collections::BTreeSet;
|
||||
///
|
||||
/// let set: BTreeSet<usize> = [3, 1, 2].iter().cloned().collect();
|
||||
/// let set = BTreeSet::from([3, 1, 2]);
|
||||
/// let mut set_iter = set.iter();
|
||||
/// assert_eq!(set_iter.next(), Some(&1));
|
||||
/// assert_eq!(set_iter.next(), Some(&2));
|
||||
@ -1124,7 +1123,7 @@ impl<T> IntoIterator for BTreeSet<T> {
|
||||
/// ```
|
||||
/// use std::collections::BTreeSet;
|
||||
///
|
||||
/// let set: BTreeSet<usize> = [1, 2, 3, 4].iter().cloned().collect();
|
||||
/// let set = BTreeSet::from([1, 2, 3, 4]);
|
||||
///
|
||||
/// let v: Vec<_> = set.into_iter().collect();
|
||||
/// assert_eq!(v, [1, 2, 3, 4]);
|
||||
@ -1243,8 +1242,8 @@ impl<T: Ord + Clone> Sub<&BTreeSet<T>> for &BTreeSet<T> {
|
||||
/// ```
|
||||
/// use std::collections::BTreeSet;
|
||||
///
|
||||
/// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
|
||||
/// let b: BTreeSet<_> = vec![3, 4, 5].into_iter().collect();
|
||||
/// let a = BTreeSet::from([1, 2, 3]);
|
||||
/// let b = BTreeSet::from([3, 4, 5]);
|
||||
///
|
||||
/// let result = &a - &b;
|
||||
/// let result_vec: Vec<_> = result.into_iter().collect();
|
||||
@ -1266,8 +1265,8 @@ impl<T: Ord + Clone> BitXor<&BTreeSet<T>> for &BTreeSet<T> {
|
||||
/// ```
|
||||
/// use std::collections::BTreeSet;
|
||||
///
|
||||
/// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
|
||||
/// let b: BTreeSet<_> = vec![2, 3, 4].into_iter().collect();
|
||||
/// let a = BTreeSet::from([1, 2, 3]);
|
||||
/// let b = BTreeSet::from([2, 3, 4]);
|
||||
///
|
||||
/// let result = &a ^ &b;
|
||||
/// let result_vec: Vec<_> = result.into_iter().collect();
|
||||
@ -1289,8 +1288,8 @@ impl<T: Ord + Clone> BitAnd<&BTreeSet<T>> for &BTreeSet<T> {
|
||||
/// ```
|
||||
/// use std::collections::BTreeSet;
|
||||
///
|
||||
/// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
|
||||
/// let b: BTreeSet<_> = vec![2, 3, 4].into_iter().collect();
|
||||
/// let a = BTreeSet::from([1, 2, 3]);
|
||||
/// let b = BTreeSet::from([2, 3, 4]);
|
||||
///
|
||||
/// let result = &a & &b;
|
||||
/// let result_vec: Vec<_> = result.into_iter().collect();
|
||||
@ -1312,8 +1311,8 @@ impl<T: Ord + Clone> BitOr<&BTreeSet<T>> for &BTreeSet<T> {
|
||||
/// ```
|
||||
/// use std::collections::BTreeSet;
|
||||
///
|
||||
/// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
|
||||
/// let b: BTreeSet<_> = vec![3, 4, 5].into_iter().collect();
|
||||
/// let a = BTreeSet::from([1, 2, 3]);
|
||||
/// let b = BTreeSet::from([3, 4, 5]);
|
||||
///
|
||||
/// let result = &a | &b;
|
||||
/// let result_vec: Vec<_> = result.into_iter().collect();
|
||||
|
@ -233,7 +233,7 @@ impl<T, S> HashSet<T, S> {
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
|
||||
/// let mut set = HashSet::from([1, 2, 3]);
|
||||
/// assert!(!set.is_empty());
|
||||
///
|
||||
/// // print 1, 2, 3 in an arbitrary order
|
||||
@ -489,8 +489,8 @@ where
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
/// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect();
|
||||
/// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
|
||||
/// let a = HashSet::from([1, 2, 3]);
|
||||
/// let b = HashSet::from([4, 2, 3, 4]);
|
||||
///
|
||||
/// // Can be seen as `a - b`.
|
||||
/// for x in a.difference(&b) {
|
||||
@ -518,8 +518,8 @@ where
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
/// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect();
|
||||
/// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
|
||||
/// let a = HashSet::from([1, 2, 3]);
|
||||
/// let b = HashSet::from([4, 2, 3, 4]);
|
||||
///
|
||||
/// // Print 1, 4 in arbitrary order.
|
||||
/// for x in a.symmetric_difference(&b) {
|
||||
@ -548,8 +548,8 @@ where
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
/// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect();
|
||||
/// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
|
||||
/// let a = HashSet::from([1, 2, 3]);
|
||||
/// let b = HashSet::from([4, 2, 3, 4]);
|
||||
///
|
||||
/// // Print 2, 3 in arbitrary order.
|
||||
/// for x in a.intersection(&b) {
|
||||
@ -576,8 +576,8 @@ where
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
/// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect();
|
||||
/// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
|
||||
/// let a = HashSet::from([1, 2, 3]);
|
||||
/// let b = HashSet::from([4, 2, 3, 4]);
|
||||
///
|
||||
/// // Print 1, 2, 3, 4 in arbitrary order.
|
||||
/// for x in a.union(&b) {
|
||||
@ -608,7 +608,7 @@ where
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
|
||||
/// let set = HashSet::from([1, 2, 3]);
|
||||
/// assert_eq!(set.contains(&1), true);
|
||||
/// assert_eq!(set.contains(&4), false);
|
||||
/// ```
|
||||
@ -633,7 +633,7 @@ where
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
|
||||
/// let set = HashSet::from([1, 2, 3]);
|
||||
/// assert_eq!(set.get(&2), Some(&2));
|
||||
/// assert_eq!(set.get(&4), None);
|
||||
/// ```
|
||||
@ -657,7 +657,7 @@ where
|
||||
///
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
|
||||
/// let mut set = HashSet::from([1, 2, 3]);
|
||||
/// assert_eq!(set.len(), 3);
|
||||
/// assert_eq!(set.get_or_insert(2), &2);
|
||||
/// assert_eq!(set.get_or_insert(100), &100);
|
||||
@ -744,7 +744,7 @@ where
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect();
|
||||
/// let a = HashSet::from([1, 2, 3]);
|
||||
/// let mut b = HashSet::new();
|
||||
///
|
||||
/// assert_eq!(a.is_disjoint(&b), true);
|
||||
@ -770,7 +770,7 @@ where
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let sup: HashSet<_> = [1, 2, 3].iter().cloned().collect();
|
||||
/// let sup = HashSet::from([1, 2, 3]);
|
||||
/// let mut set = HashSet::new();
|
||||
///
|
||||
/// assert_eq!(set.is_subset(&sup), true);
|
||||
@ -792,7 +792,7 @@ where
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let sub: HashSet<_> = [1, 2].iter().cloned().collect();
|
||||
/// let sub = HashSet::from([1, 2]);
|
||||
/// let mut set = HashSet::new();
|
||||
///
|
||||
/// assert_eq!(set.is_superset(&sub), false);
|
||||
@ -893,7 +893,7 @@ where
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
|
||||
/// let mut set = HashSet::from([1, 2, 3]);
|
||||
/// assert_eq!(set.take(&2), Some(2));
|
||||
/// assert_eq!(set.take(&2), None);
|
||||
/// ```
|
||||
@ -917,8 +917,7 @@ where
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let xs = [1, 2, 3, 4, 5, 6];
|
||||
/// let mut set: HashSet<i32> = xs.iter().cloned().collect();
|
||||
/// let mut set = HashSet::from([1, 2, 3, 4, 5, 6]);
|
||||
/// set.retain(|&k| k % 2 == 0);
|
||||
/// assert_eq!(set.len(), 3);
|
||||
/// ```
|
||||
@ -1097,8 +1096,8 @@ where
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let a: HashSet<_> = vec![1, 2, 3].into_iter().collect();
|
||||
/// let b: HashSet<_> = vec![3, 4, 5].into_iter().collect();
|
||||
/// let a = HashSet::from([1, 2, 3]);
|
||||
/// let b = HashSet::from([3, 4, 5]);
|
||||
///
|
||||
/// let set = &a | &b;
|
||||
///
|
||||
@ -1130,8 +1129,8 @@ where
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let a: HashSet<_> = vec![1, 2, 3].into_iter().collect();
|
||||
/// let b: HashSet<_> = vec![2, 3, 4].into_iter().collect();
|
||||
/// let a = HashSet::from([1, 2, 3]);
|
||||
/// let b = HashSet::from([2, 3, 4]);
|
||||
///
|
||||
/// let set = &a & &b;
|
||||
///
|
||||
@ -1163,8 +1162,8 @@ where
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let a: HashSet<_> = vec![1, 2, 3].into_iter().collect();
|
||||
/// let b: HashSet<_> = vec![3, 4, 5].into_iter().collect();
|
||||
/// let a = HashSet::from([1, 2, 3]);
|
||||
/// let b = HashSet::from([3, 4, 5]);
|
||||
///
|
||||
/// let set = &a ^ &b;
|
||||
///
|
||||
@ -1196,8 +1195,8 @@ where
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let a: HashSet<_> = vec![1, 2, 3].into_iter().collect();
|
||||
/// let b: HashSet<_> = vec![3, 4, 5].into_iter().collect();
|
||||
/// let a = HashSet::from([1, 2, 3]);
|
||||
/// let b = HashSet::from([3, 4, 5]);
|
||||
///
|
||||
/// let set = &a - &b;
|
||||
///
|
||||
@ -1226,7 +1225,7 @@ where
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
|
||||
/// let a = HashSet::from([1, 2, 3]);
|
||||
///
|
||||
/// let mut iter = a.iter();
|
||||
/// ```
|
||||
@ -1248,7 +1247,7 @@ pub struct Iter<'a, K: 'a> {
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
|
||||
/// let a = HashSet::from([1, 2, 3]);
|
||||
///
|
||||
/// let mut iter = a.into_iter();
|
||||
/// ```
|
||||
@ -1269,7 +1268,7 @@ pub struct IntoIter<K> {
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let mut a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
|
||||
/// let mut a = HashSet::from([1, 2, 3]);
|
||||
///
|
||||
/// let mut drain = a.drain();
|
||||
/// ```
|
||||
@ -1291,7 +1290,7 @@ pub struct Drain<'a, K: 'a> {
|
||||
///
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let mut a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
|
||||
/// let mut a = HashSet::from([1, 2, 3]);
|
||||
///
|
||||
/// let mut drain_filtered = a.drain_filter(|v| v % 2 == 0);
|
||||
/// ```
|
||||
@ -1315,8 +1314,8 @@ where
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
|
||||
/// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
|
||||
/// let a = HashSet::from([1, 2, 3]);
|
||||
/// let b = HashSet::from([4, 2, 3, 4]);
|
||||
///
|
||||
/// let mut intersection = a.intersection(&b);
|
||||
/// ```
|
||||
@ -1342,8 +1341,8 @@ pub struct Intersection<'a, T: 'a, S: 'a> {
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
|
||||
/// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
|
||||
/// let a = HashSet::from([1, 2, 3]);
|
||||
/// let b = HashSet::from([4, 2, 3, 4]);
|
||||
///
|
||||
/// let mut difference = a.difference(&b);
|
||||
/// ```
|
||||
@ -1369,8 +1368,8 @@ pub struct Difference<'a, T: 'a, S: 'a> {
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
|
||||
/// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
|
||||
/// let a = HashSet::from([1, 2, 3]);
|
||||
/// let b = HashSet::from([4, 2, 3, 4]);
|
||||
///
|
||||
/// let mut intersection = a.symmetric_difference(&b);
|
||||
/// ```
|
||||
@ -1393,8 +1392,8 @@ pub struct SymmetricDifference<'a, T: 'a, S: 'a> {
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
|
||||
/// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
|
||||
/// let a = HashSet::from([1, 2, 3]);
|
||||
/// let b = HashSet::from([4, 2, 3, 4]);
|
||||
///
|
||||
/// let mut union_iter = a.union(&b);
|
||||
/// ```
|
||||
|
Loading…
Reference in New Issue
Block a user