6 let xs = [0, 1, 2, 4, 5];
9 let mut it = xs.iter().zip(&ys);
10 assert_eq!(it.nth(0), Some((&0, &10)));
11 assert_eq!(it.nth(1), Some((&2, &12)));
12 assert_eq!(it.nth(0), None);
14 let mut it = xs.iter().zip(&ys);
15 assert_eq!(it.nth(3), None);
17 let mut it = ys.iter().zip(&xs);
18 assert_eq!(it.nth(3), None);
22 fn test_zip_nth_side_effects() {
23 let mut a = Vec::new();
24 let mut b = Vec::new();
25 let value = [1, 2, 3, 4, 5, 6]
32 .zip([2, 3, 4, 5, 6, 7, 8].iter().cloned().map(|n| {
38 assert_eq!(value, Some((50, 6000)));
39 assert_eq!(a, vec![1, 2, 3, 4, 5]);
40 assert_eq!(b, vec![200, 300, 400, 500, 600]);
44 fn test_zip_next_back_side_effects() {
45 let mut a = Vec::new();
46 let mut b = Vec::new();
47 let mut iter = [1, 2, 3, 4, 5, 6]
54 .zip([2, 3, 4, 5, 6, 7, 8].iter().cloned().map(|n| {
59 // The second iterator is one item longer, so `next_back` is called on it
61 assert_eq!(iter.next_back(), Some((60, 7000)));
62 assert_eq!(iter.next_back(), Some((50, 6000)));
63 assert_eq!(iter.next_back(), Some((40, 5000)));
64 assert_eq!(iter.next_back(), Some((30, 4000)));
65 assert_eq!(a, vec![6, 5, 4, 3]);
66 assert_eq!(b, vec![800, 700, 600, 500, 400]);
70 fn test_zip_nth_back_side_effects() {
71 let mut a = Vec::new();
72 let mut b = Vec::new();
73 let value = [1, 2, 3, 4, 5, 6]
80 .zip([2, 3, 4, 5, 6, 7, 8].iter().cloned().map(|n| {
85 assert_eq!(value, Some((30, 4000)));
86 assert_eq!(a, vec![6, 5, 4, 3]);
87 assert_eq!(b, vec![800, 700, 600, 500, 400]);
91 fn test_zip_next_back_side_effects_exhausted() {
92 let mut a = Vec::new();
93 let mut b = Vec::new();
94 let mut iter = [1, 2, 3, 4, 5, 6]
101 .zip([2, 3, 4].iter().cloned().map(|n| {
110 assert_eq!(iter.next_back(), None);
111 assert_eq!(a, vec![1, 2, 3, 4, 6, 5]);
112 assert_eq!(b, vec![200, 300, 400]);
116 fn test_zip_cloned_sideffectful() {
117 let xs = [CountClone::new(), CountClone::new(), CountClone::new(), CountClone::new()];
118 let ys = [CountClone::new(), CountClone::new()];
120 for _ in xs.iter().cloned().zip(ys.iter().cloned()) {}
122 assert_eq!(&xs, &[1, 1, 1, 0][..]);
123 assert_eq!(&ys, &[1, 1][..]);
125 let xs = [CountClone::new(), CountClone::new()];
126 let ys = [CountClone::new(), CountClone::new(), CountClone::new(), CountClone::new()];
128 for _ in xs.iter().cloned().zip(ys.iter().cloned()) {}
130 assert_eq!(&xs, &[1, 1][..]);
131 assert_eq!(&ys, &[1, 1, 0, 0][..]);
135 fn test_zip_map_sideffectful() {
139 for _ in xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1)) {}
141 assert_eq!(&xs, &[1, 1, 1, 1, 1, 0]);
142 assert_eq!(&ys, &[1, 1, 1, 1]);
147 for _ in xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1)) {}
149 assert_eq!(&xs, &[1, 1, 1, 1]);
150 assert_eq!(&ys, &[1, 1, 1, 1, 0, 0]);
154 fn test_zip_map_rev_sideffectful() {
159 let mut it = xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1));
162 assert_eq!(&xs, &[0, 0, 0, 1, 1, 1]);
163 assert_eq!(&ys, &[0, 0, 0, 1]);
169 let mut it = xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1));
170 (&mut it).take(5).count();
173 assert_eq!(&xs, &[1, 1, 1, 1, 1, 1]);
174 assert_eq!(&ys, &[1, 1, 1, 1]);
178 fn test_zip_nested_sideffectful() {
183 // test that it has the side effect nested inside enumerate
184 let it = xs.iter_mut().map(|x| *x = 1).enumerate().zip(&ys);
187 assert_eq!(&xs, &[1, 1, 1, 1, 1, 0]);
191 fn test_zip_nth_back_side_effects_exhausted() {
192 let mut a = Vec::new();
193 let mut b = Vec::new();
194 let mut iter = [1, 2, 3, 4, 5, 6]
201 .zip([2, 3, 4].iter().cloned().map(|n| {
210 assert_eq!(iter.nth_back(0), None);
211 assert_eq!(a, vec![1, 2, 3, 4, 6, 5]);
212 assert_eq!(b, vec![200, 300, 400]);
216 fn test_zip_trusted_random_access_composition() {
217 let a = [0, 1, 2, 3, 4];
221 let a = a.iter().copied();
222 let b = b.iter().copied();
223 let mut c = c.iter().copied();
226 let mut z1 = a.zip(b);
227 assert_eq!(z1.next().unwrap(), (0, 0));
229 let mut z2 = z1.zip(c);
230 fn assert_trusted_random_access<T: TrustedRandomAccess>(_a: &T) {}
231 assert_trusted_random_access(&z2);
232 assert_eq!(z2.next().unwrap(), ((1, 1), 1));
236 #[cfg(panic = "unwind")]
237 fn test_zip_trusted_random_access_next_back_drop() {
238 use std::panic::catch_unwind;
239 use std::panic::AssertUnwindSafe;
243 let it = [42].iter().map(|e| {
252 let it2 = [(); 0].iter();
253 let mut zip = it.zip(it2);
254 catch_unwind(AssertUnwindSafe(|| {
258 assert!(zip.next().is_none());
259 assert_eq!(counter, 1);
263 fn test_double_ended_zip() {
264 let xs = [1, 2, 3, 4, 5, 6];
265 let ys = [1, 2, 3, 7];
266 let mut it = xs.iter().cloned().zip(ys);
267 assert_eq!(it.next(), Some((1, 1)));
268 assert_eq!(it.next(), Some((2, 2)));
269 assert_eq!(it.next_back(), Some((4, 7)));
270 assert_eq!(it.next_back(), Some((3, 3)));
271 assert_eq!(it.next(), None);
275 fn test_issue_82282() {
276 fn overflowed_zip(arr: &[i32]) -> impl Iterator<Item = (i32, &())> {
277 static UNIT_EMPTY_ARR: [(); 0] = [];
279 let mapped = arr.into_iter().map(|i| *i);
280 let mut zipped = mapped.zip(UNIT_EMPTY_ARR.iter());
286 let zip = overflowed_zip(&arr).zip(overflowed_zip(&arr));
288 assert_eq!(zip.size_hint(), (0, Some(0)));
295 fn test_issue_82291() {
301 let called = Cell::new(0);
306 called.set(called.get() + 1);
312 assert_eq!(called.get(), 1);
314 assert_eq!(called.get(), 1);