Skip to content

Commit e75a656

Browse files
committed
Added body to free::zip
Added body to the code example in the docs for free::zip. The code body added uses two types to make it obvious how `zip` functions.
1 parent 6b7063a commit e75a656

File tree

1 file changed

+60
-46
lines changed

1 file changed

+60
-46
lines changed

src/free.rs

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,24 @@ use std::iter::{self, Zip};
1010
type VecIntoIter<T> = alloc::vec::IntoIter<T>;
1111

1212
#[cfg(feature = "use_alloc")]
13-
use alloc::{
14-
string::String,
15-
};
13+
use alloc::string::String;
1614

17-
use crate::Itertools;
1815
use crate::intersperse::{Intersperse, IntersperseWith};
16+
use crate::Itertools;
1917

20-
pub use crate::adaptors::{
21-
interleave,
22-
merge,
23-
put_back,
24-
};
18+
pub use crate::adaptors::{interleave, merge, put_back};
2519
#[cfg(feature = "use_alloc")]
26-
pub use crate::put_back_n_impl::put_back_n;
20+
pub use crate::kmerge_impl::kmerge;
21+
pub use crate::merge_join::merge_join_by;
2722
#[cfg(feature = "use_alloc")]
2823
pub use crate::multipeek_impl::multipeek;
2924
#[cfg(feature = "use_alloc")]
3025
pub use crate::peek_nth::peek_nth;
3126
#[cfg(feature = "use_alloc")]
32-
pub use crate::kmerge_impl::kmerge;
33-
pub use crate::zip_eq_impl::zip_eq;
34-
pub use crate::merge_join::merge_join_by;
27+
pub use crate::put_back_n_impl::put_back_n;
3528
#[cfg(feature = "use_alloc")]
3629
pub use crate::rciter_impl::rciter;
30+
pub use crate::zip_eq_impl::zip_eq;
3731

3832
/// Iterate `iterable` with a particular value inserted between each element.
3933
///
@@ -45,8 +39,9 @@ pub use crate::rciter_impl::rciter;
4539
/// itertools::assert_equal(intersperse((0..3), 8), vec![0, 8, 1, 8, 2]);
4640
/// ```
4741
pub fn intersperse<I>(iterable: I, element: I::Item) -> Intersperse<I::IntoIter>
48-
where I: IntoIterator,
49-
<I as IntoIterator>::Item: Clone
42+
where
43+
I: IntoIterator,
44+
<I as IntoIterator>::Item: Clone,
5045
{
5146
Itertools::intersperse(iterable.into_iter(), element)
5247
}
@@ -64,8 +59,9 @@ pub fn intersperse<I>(iterable: I, element: I::Item) -> Intersperse<I::IntoIter>
6459
/// assert_eq!(i, 8);
6560
/// ```
6661
pub fn intersperse_with<I, F>(iterable: I, element: F) -> IntersperseWith<I::IntoIter, F>
67-
where I: IntoIterator,
68-
F: FnMut() -> I::Item
62+
where
63+
I: IntoIterator,
64+
F: FnMut() -> I::Item,
6965
{
7066
Itertools::intersperse_with(iterable.into_iter(), element)
7167
}
@@ -82,7 +78,8 @@ pub fn intersperse_with<I, F>(iterable: I, element: F) -> IntersperseWith<I::Int
8278
/// }
8379
/// ```
8480
pub fn enumerate<I>(iterable: I) -> iter::Enumerate<I::IntoIter>
85-
where I: IntoIterator
81+
where
82+
I: IntoIterator,
8683
{
8784
iterable.into_iter().enumerate()
8885
}
@@ -99,8 +96,9 @@ pub fn enumerate<I>(iterable: I) -> iter::Enumerate<I::IntoIter>
9996
/// }
10097
/// ```
10198
pub fn rev<I>(iterable: I) -> iter::Rev<I::IntoIter>
102-
where I: IntoIterator,
103-
I::IntoIter: DoubleEndedIterator
99+
where
100+
I: IntoIterator,
101+
I::IntoIter: DoubleEndedIterator,
104102
{
105103
iterable.into_iter().rev()
106104
}
@@ -112,14 +110,20 @@ pub fn rev<I>(iterable: I) -> iter::Rev<I::IntoIter>
112110
/// ```
113111
/// use itertools::zip;
114112
///
115-
/// let data = [1, 2, 3, 4, 5];
116-
/// for (a, b) in zip(&data, &data[1..]) {
117-
/// /* loop body */
113+
/// let data_1 = [1, 2, 3, 4, 5];
114+
/// let data_2 = ['a', 'b', 'c'];
115+
/// let mut result: Vec<(i32, char)> = Vec::new();
116+
///
117+
/// for (a, b) in zip(&data_1, &data_2) {
118+
/// result.push((*a, *b));
118119
/// }
120+
/// assert_eq!(result, vec![(1, 'a'),(2, 'b'),(3, 'c')]);
119121
/// ```
122+
///
120123
pub fn zip<I, J>(i: I, j: J) -> Zip<I::IntoIter, J::IntoIter>
121-
where I: IntoIterator,
122-
J: IntoIterator
124+
where
125+
I: IntoIterator,
126+
J: IntoIterator,
123127
{
124128
i.into_iter().zip(j)
125129
}
@@ -135,9 +139,13 @@ pub fn zip<I, J>(i: I, j: J) -> Zip<I::IntoIter, J::IntoIter>
135139
/// /* loop body */
136140
/// }
137141
/// ```
138-
pub fn chain<I, J>(i: I, j: J) -> iter::Chain<<I as IntoIterator>::IntoIter, <J as IntoIterator>::IntoIter>
139-
where I: IntoIterator,
140-
J: IntoIterator<Item = I::Item>
142+
pub fn chain<I, J>(
143+
i: I,
144+
j: J,
145+
) -> iter::Chain<<I as IntoIterator>::IntoIter, <J as IntoIterator>::IntoIter>
146+
where
147+
I: IntoIterator,
148+
J: IntoIterator<Item = I::Item>,
141149
{
142150
i.into_iter().chain(j)
143151
}
@@ -152,8 +160,9 @@ pub fn chain<I, J>(i: I, j: J) -> iter::Chain<<I as IntoIterator>::IntoIter, <J
152160
/// assert_eq!(cloned(b"abc").next(), Some(b'a'));
153161
/// ```
154162
pub fn cloned<'a, I, T: 'a>(iterable: I) -> iter::Cloned<I::IntoIter>
155-
where I: IntoIterator<Item=&'a T>,
156-
T: Clone,
163+
where
164+
I: IntoIterator<Item = &'a T>,
165+
T: Clone,
157166
{
158167
iterable.into_iter().cloned()
159168
}
@@ -168,8 +177,9 @@ pub fn cloned<'a, I, T: 'a>(iterable: I) -> iter::Cloned<I::IntoIter>
168177
/// assert_eq!(fold(&[1., 2., 3.], 0., |a, &b| f32::max(a, b)), 3.);
169178
/// ```
170179
pub fn fold<I, B, F>(iterable: I, init: B, f: F) -> B
171-
where I: IntoIterator,
172-
F: FnMut(B, I::Item) -> B
180+
where
181+
I: IntoIterator,
182+
F: FnMut(B, I::Item) -> B,
173183
{
174184
iterable.into_iter().fold(init, f)
175185
}
@@ -184,8 +194,9 @@ pub fn fold<I, B, F>(iterable: I, init: B, f: F) -> B
184194
/// assert!(all(&[1, 2, 3], |elt| *elt > 0));
185195
/// ```
186196
pub fn all<I, F>(iterable: I, f: F) -> bool
187-
where I: IntoIterator,
188-
F: FnMut(I::Item) -> bool
197+
where
198+
I: IntoIterator,
199+
F: FnMut(I::Item) -> bool,
189200
{
190201
iterable.into_iter().all(f)
191202
}
@@ -200,8 +211,9 @@ pub fn all<I, F>(iterable: I, f: F) -> bool
200211
/// assert!(any(&[0, -1, 2], |elt| *elt > 0));
201212
/// ```
202213
pub fn any<I, F>(iterable: I, f: F) -> bool
203-
where I: IntoIterator,
204-
F: FnMut(I::Item) -> bool
214+
where
215+
I: IntoIterator,
216+
F: FnMut(I::Item) -> bool,
205217
{
206218
iterable.into_iter().any(f)
207219
}
@@ -216,8 +228,9 @@ pub fn any<I, F>(iterable: I, f: F) -> bool
216228
/// assert_eq!(max(0..10), Some(9));
217229
/// ```
218230
pub fn max<I>(iterable: I) -> Option<I::Item>
219-
where I: IntoIterator,
220-
I::Item: Ord
231+
where
232+
I: IntoIterator,
233+
I::Item: Ord,
221234
{
222235
iterable.into_iter().max()
223236
}
@@ -232,13 +245,13 @@ pub fn max<I>(iterable: I) -> Option<I::Item>
232245
/// assert_eq!(min(0..10), Some(0));
233246
/// ```
234247
pub fn min<I>(iterable: I) -> Option<I::Item>
235-
where I: IntoIterator,
236-
I::Item: Ord
248+
where
249+
I: IntoIterator,
250+
I::Item: Ord,
237251
{
238252
iterable.into_iter().min()
239253
}
240254

241-
242255
/// Combine all iterator elements into one String, separated by `sep`.
243256
///
244257
/// [`IntoIterator`] enabled version of [`Itertools::join`].
@@ -250,8 +263,9 @@ pub fn min<I>(iterable: I) -> Option<I::Item>
250263
/// ```
251264
#[cfg(feature = "use_alloc")]
252265
pub fn join<I>(iterable: I, sep: &str) -> String
253-
where I: IntoIterator,
254-
I::Item: Display
266+
where
267+
I: IntoIterator,
268+
I::Item: Display,
255269
{
256270
iterable.into_iter().join(sep)
257271
}
@@ -268,9 +282,9 @@ pub fn join<I>(iterable: I, sep: &str) -> String
268282
/// ```
269283
#[cfg(feature = "use_alloc")]
270284
pub fn sorted<I>(iterable: I) -> VecIntoIter<I::Item>
271-
where I: IntoIterator,
272-
I::Item: Ord
285+
where
286+
I: IntoIterator,
287+
I::Item: Ord,
273288
{
274289
iterable.into_iter().sorted()
275290
}
276-

0 commit comments

Comments
 (0)