Update expressions where we can use array's IntoIterator implementation

This commit is contained in:
Muhammad Mominul Huque 2021-06-02 16:06:34 +06:00
parent 01d4d46f66
commit 507d97b26e
7 changed files with 16 additions and 24 deletions

View File

@ -551,19 +551,13 @@ fn bench_in_place_collect_droppable(b: &mut Bencher) {
#[bench]
fn bench_chain_collect(b: &mut Bencher) {
let data = black_box([0; LEN]);
b.iter(|| data.iter().cloned().chain([1].iter().cloned()).collect::<Vec<_>>());
b.iter(|| data.iter().cloned().chain([1]).collect::<Vec<_>>());
}
#[bench]
fn bench_chain_chain_collect(b: &mut Bencher) {
let data = black_box([0; LEN]);
b.iter(|| {
data.iter()
.cloned()
.chain([1].iter().cloned())
.chain([2].iter().cloned())
.collect::<Vec<_>>()
});
b.iter(|| data.iter().cloned().chain([1]).chain([2]).collect::<Vec<_>>());
}
#[bench]

View File

@ -921,7 +921,7 @@ pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveE
///
/// ```
/// let mut vec = Vec::with_capacity(10);
/// vec.extend([1, 2, 3].iter().cloned());
/// vec.extend([1, 2, 3]);
/// assert_eq!(vec.capacity(), 10);
/// vec.shrink_to_fit();
/// assert!(vec.capacity() >= 3);
@ -950,7 +950,7 @@ pub fn shrink_to_fit(&mut self) {
/// ```
/// #![feature(shrink_to)]
/// let mut vec = Vec::with_capacity(10);
/// vec.extend([1, 2, 3].iter().cloned());
/// vec.extend([1, 2, 3]);
/// assert_eq!(vec.capacity(), 10);
/// vec.shrink_to(4);
/// assert!(vec.capacity() >= 4);
@ -984,7 +984,7 @@ pub fn shrink_to(&mut self, min_capacity: usize) {
///
/// ```
/// let mut vec = Vec::with_capacity(10);
/// vec.extend([1, 2, 3].iter().cloned());
/// vec.extend([1, 2, 3]);
///
/// assert_eq!(vec.capacity(), 10);
/// let slice = vec.into_boxed_slice();
@ -2586,7 +2586,7 @@ fn extend_desugared<I: Iterator<Item = T>>(&mut self, mut iterator: I) {
/// ```
/// let mut v = vec![1, 2, 3];
/// let new = [7, 8];
/// let u: Vec<_> = v.splice(..2, new.iter().cloned()).collect();
/// let u: Vec<_> = v.splice(..2, new).collect();
/// assert_eq!(v, &[7, 8, 3]);
/// assert_eq!(u, &[1, 2]);
/// ```

View File

@ -14,7 +14,7 @@
/// ```
/// let mut v = vec![0, 1, 2];
/// let new = [7, 8];
/// let iter: std::vec::Splice<_> = v.splice(1.., new.iter().cloned());
/// let iter: std::vec::Splice<_> = v.splice(1.., new);
/// ```
#[derive(Debug)]
#[stable(feature = "vec_splice", since = "1.21.0")]

View File

@ -793,7 +793,7 @@ fn drop(&mut self) {
fn test_splice() {
let mut v = vec![1, 2, 3, 4, 5];
let a = [10, 11, 12];
v.splice(2..4, a.iter().cloned());
v.splice(2..4, a);
assert_eq!(v, &[1, 2, 10, 11, 12, 5]);
v.splice(1..3, Some(20));
assert_eq!(v, &[1, 20, 11, 12, 5]);
@ -803,7 +803,7 @@ fn test_splice() {
fn test_splice_inclusive_range() {
let mut v = vec![1, 2, 3, 4, 5];
let a = [10, 11, 12];
let t1: Vec<_> = v.splice(2..=3, a.iter().cloned()).collect();
let t1: Vec<_> = v.splice(2..=3, a).collect();
assert_eq!(v, &[1, 2, 10, 11, 12, 5]);
assert_eq!(t1, &[3, 4]);
let t2: Vec<_> = v.splice(1..=2, Some(20)).collect();
@ -816,7 +816,7 @@ fn test_splice_inclusive_range() {
fn test_splice_out_of_bounds() {
let mut v = vec![1, 2, 3, 4, 5];
let a = [10, 11, 12];
v.splice(5..6, a.iter().cloned());
v.splice(5..6, a);
}
#[test]
@ -824,7 +824,7 @@ fn test_splice_out_of_bounds() {
fn test_splice_inclusive_out_of_bounds() {
let mut v = vec![1, 2, 3, 4, 5];
let a = [10, 11, 12];
v.splice(5..=5, a.iter().cloned());
v.splice(5..=5, a);
}
#[test]
@ -848,7 +848,7 @@ fn test_splice_unbounded() {
fn test_splice_forget() {
let mut v = vec![1, 2, 3, 4, 5];
let a = [10, 11, 12];
std::mem::forget(v.splice(2..4, a.iter().cloned()));
std::mem::forget(v.splice(2..4, a));
assert_eq!(v, &[1, 2]);
}

View File

@ -58,7 +58,7 @@ impl char {
/// ];
///
/// assert_eq!(
/// decode_utf16(v.iter().cloned())
/// decode_utf16(v)
/// .map(|r| r.map_err(|e| e.unpaired_surrogate()))
/// .collect::<Vec<_>>(),
/// vec![
@ -82,7 +82,7 @@ impl char {
/// ];
///
/// assert_eq!(
/// decode_utf16(v.iter().cloned())
/// decode_utf16(v)
/// .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))
/// .collect::<String>(),
/// "𝄞mus<75>ic<69>"

View File

@ -1,4 +1,4 @@
use core::array::{self, IntoIter};
use core::array;
use core::convert::TryFrom;
#[test]

View File

@ -236,9 +236,7 @@ fn assert_trusted_random_access<T: TrustedRandomAccess>(_a: &T) {}
fn test_double_ended_zip() {
let xs = [1, 2, 3, 4, 5, 6];
let ys = [1, 2, 3, 7];
let a = xs.iter().cloned();
let b = ys.iter().cloned();
let mut it = a.zip(b);
let mut it = xs.iter().cloned().zip(ys);
assert_eq!(it.next(), Some((1, 1)));
assert_eq!(it.next(), Some((2, 2)));
assert_eq!(it.next_back(), Some((4, 7)));