|
1 |
| -use crate::iter::adapters::{Map, mapped_iterator}; |
| 1 | +use crate::iter::adapters::{Map, Zip, mapped_iterator, zipped_iterator}; |
2 | 2 |
|
3 | 3 | /// A trait for dealing with iterators.
|
4 | 4 | ///
|
@@ -93,4 +93,79 @@ pub trait Iterator<T> {
|
93 | 93 | ) -> Map<T, F> {
|
94 | 94 | mapped_iterator(self, f)
|
95 | 95 | }
|
| 96 | + |
| 97 | + /// 'Zips up' two iterators into a single iterator of pairs. |
| 98 | + /// |
| 99 | + /// `zip()` returns a new iterator that will iterate over two other |
| 100 | + /// iterators, returning a tuple where the first element comes from the |
| 101 | + /// first iterator, and the second element comes from the second iterator. |
| 102 | + /// |
| 103 | + /// In other words, it zips two iterators together, into a single one. |
| 104 | + /// |
| 105 | + /// If either iterator returns [`Option::None`], [`next`] from the zipped iterator |
| 106 | + /// will return [`Option::None`]. |
| 107 | + /// If the zipped iterator has no more elements to return then each further attempt to advance |
| 108 | + /// it will first try to advance the first iterator at most one time and if it still yielded an |
| 109 | + /// item try to advance the second iterator at most one time. |
| 110 | + /// |
| 111 | + /// # Examples |
| 112 | + /// |
| 113 | + /// Basic usage: |
| 114 | + /// |
| 115 | + /// ``` |
| 116 | + /// let a1 = array![1, 2, 3]; |
| 117 | + /// let a2 = array![4, 5, 6]; |
| 118 | + /// |
| 119 | + /// let mut iter = a1.into_iter().zip(a2.into_iter()); |
| 120 | + /// |
| 121 | + /// assert_eq!(iter.next(), Option::Some((1, 4))); |
| 122 | + /// assert_eq!(iter.next(), Option::Some((2, 5))); |
| 123 | + /// assert_eq!(iter.next(), Option::Some((3, 6))); |
| 124 | + /// assert_eq!(iter.next(), Option::None); |
| 125 | + /// ``` |
| 126 | + /// |
| 127 | + /// Since the argument to `zip()` uses [`IntoIterator`], we can pass |
| 128 | + /// anything that can be converted into an [`Iterator`], not just an |
| 129 | + /// [`Iterator`] itself. For example: |
| 130 | + /// |
| 131 | + /// ``` |
| 132 | + /// let a1 = array![1, 2, 3]; |
| 133 | + /// let a2 = array![4, 5, 6]; |
| 134 | + /// |
| 135 | + /// let mut iter = a1.into_iter().zip(a2); |
| 136 | + /// |
| 137 | + /// assert_eq!(iter.next(), Option::Some((1, 4))); |
| 138 | + /// assert_eq!(iter.next(), Option::Some((2, 5))); |
| 139 | + /// assert_eq!(iter.next(), Option::Some((3, 6))); |
| 140 | + /// assert_eq!(iter.next(), Option::None); |
| 141 | + /// ``` |
| 142 | + /// |
| 143 | + /// If both iterators have roughly equivalent syntax, it may be more readable to use [`zip`]: |
| 144 | + /// |
| 145 | + /// ``` |
| 146 | + /// use core::iter::zip; |
| 147 | + /// |
| 148 | + /// let a = array![1, 2, 3]; |
| 149 | + /// let b = array![2, 3, 4]; |
| 150 | + /// |
| 151 | + /// let mut zipped = zip(a, b); |
| 152 | + /// |
| 153 | + /// assert_eq!(iter.next(), Option::Some((1, 4))); |
| 154 | + /// assert_eq!(iter.next(), Option::Some((2, 5))); |
| 155 | + /// assert_eq!(iter.next(), Option::Some((3, 6))); |
| 156 | + /// assert_eq!(iter.next(), Option::None); |
| 157 | + /// ); |
| 158 | + /// ``` |
| 159 | + /// |
| 160 | + /// [`enumerate`]: Iterator::enumerate |
| 161 | + /// [`next`]: Iterator::next |
| 162 | + /// [`zip`]: core::iter::zip |
| 163 | + #[inline] |
| 164 | + fn zip<U, +Iterator<U> //, +IntoIterator<U> |
| 165 | + >( |
| 166 | + self: T, other: U, |
| 167 | + ) -> Zip<T, U> { |
| 168 | + zipped_iterator(self, other //.into_iter() |
| 169 | + ) |
| 170 | + } |
96 | 171 | }
|
0 commit comments