Skip to content

Commit 8419edf

Browse files
authored
feat(corelib): Iterator::collect (#7086)
1 parent 02910a1 commit 8419edf

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

corelib/src/iter/traits/collect.cairo

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
///
6666
/// assert_eq!(c.arr, array![0, 1, 2, 3, 4]);
6767
/// ```
68-
pub trait FromIterator<T, G> {
68+
pub trait FromIterator<T, A> {
6969
/// Creates a value from an iterator.
7070
///
7171
/// See the [module-level documentation] for more.
@@ -81,7 +81,7 @@ pub trait FromIterator<T, G> {
8181
///
8282
/// assert_eq!(v, array![0, 1, 2, 3, 4]);
8383
/// ```
84-
fn from_iter<I, +Iterator<I>[Item: G], +Destruct<I>>(iter: I) -> T;
84+
fn from_iter<I, +Iterator<I>[Item: A], +Destruct<I>>(iter: I) -> T;
8585
}
8686

8787
/// Conversion into an [`Iterator`].

corelib/src/iter/traits/iterator.cairo

+55
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,59 @@ pub trait Iterator<T> {
312312
) -> Zip<T, UIntoIter::IntoIter> {
313313
zipped_iterator(self, other.into_iter())
314314
}
315+
316+
/// Transforms an iterator into a collection.
317+
///
318+
/// `collect()` can take anything iterable, and turn it into a relevant
319+
/// collection. This is one of the more powerful methods in the core
320+
/// library, used in a variety of contexts.
321+
///
322+
/// The most basic pattern in which `collect()` is used is to turn one
323+
/// collection into another. You take a collection, call [`iter`] on it,
324+
/// do a bunch of transformations, and then `collect()` at the end.
325+
///
326+
/// `collect()` can also create instances of types that are not typical
327+
/// collections.
328+
///
329+
/// Because `collect()` is so general, it can cause problems with type
330+
/// inference. As such, `collect()` is one of the few times you'll see
331+
/// the syntax affectionately known as the 'turbofish': `::<>`. This
332+
/// helps the inference algorithm understand specifically which collection
333+
/// you're trying to collect into.
334+
///
335+
/// # Examples
336+
///
337+
/// Basic usage:
338+
///
339+
/// ```
340+
/// let doubled: Array<u32> = array![1, 2, 3].into_iter().map(|x| x * 2).collect();
341+
///
342+
/// assert_eq!(array![2, 4, 6], doubled);
343+
/// ```
344+
///
345+
/// Note that we needed the `: Array<u32>` on the left-hand side.
346+
///
347+
/// Using the 'turbofish' instead of annotating `doubled`:
348+
///
349+
/// ```
350+
/// let doubled = array![1, 2, 3].into_iter().map(|x| x * 2).collect::<Array<u32>>();
351+
///
352+
/// assert_eq!(array![2, 4, 6], doubled);
353+
/// ```
354+
///
355+
/// Because `collect()` only cares about what you're collecting into, you can
356+
/// still use a partial type hint, `_`, with the turbofish:
357+
///
358+
/// ```
359+
/// let doubled = array![1, 2, 3].into_iter().map(|x| x * 2).collect::<Array<_>>();
360+
///
361+
/// assert_eq!(array![2, 4, 6], doubled);
362+
/// ```
363+
#[inline]
364+
#[must_use]
365+
fn collect<B, +FromIterator<B, Self::Item>, +Destruct<T>>(
366+
self: T,
367+
) -> B {
368+
FromIterator::<B, Self::Item>::from_iter::<T, Self>(self)
369+
}
315370
}

corelib/src/test/iter_test.cairo

+5
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,8 @@ fn test_iter_adapter_fold() {
5353

5454
assert_eq!(sum, 6);
5555
}
56+
57+
#[test]
58+
fn test_iter_adapter_collect() {
59+
assert_eq!((0..3_u32).into_iter().collect(), array![0, 1, 2]);
60+
}

0 commit comments

Comments
 (0)