diff --git a/corelib/src/iter/traits/collect.cairo b/corelib/src/iter/traits/collect.cairo index c6078c4ca7d..a8a1ef534b1 100644 --- a/corelib/src/iter/traits/collect.cairo +++ b/corelib/src/iter/traits/collect.cairo @@ -65,7 +65,7 @@ /// /// assert_eq!(c.arr, array![0, 1, 2, 3, 4]); /// ``` -pub trait FromIterator { +pub trait FromIterator { /// Creates a value from an iterator. /// /// See the [module-level documentation] for more. @@ -81,7 +81,7 @@ pub trait FromIterator { /// /// assert_eq!(v, array![0, 1, 2, 3, 4]); /// ``` - fn from_iter[Item: G], +Destruct>(iter: I) -> T; + fn from_iter[Item: A], +Destruct>(iter: I) -> T; } /// Conversion into an [`Iterator`]. diff --git a/corelib/src/iter/traits/iterator.cairo b/corelib/src/iter/traits/iterator.cairo index b90a337154e..b260ef2edfe 100644 --- a/corelib/src/iter/traits/iterator.cairo +++ b/corelib/src/iter/traits/iterator.cairo @@ -312,4 +312,59 @@ pub trait Iterator { ) -> Zip { zipped_iterator(self, other.into_iter()) } + + /// Transforms an iterator into a collection. + /// + /// `collect()` can take anything iterable, and turn it into a relevant + /// collection. This is one of the more powerful methods in the core + /// library, used in a variety of contexts. + /// + /// The most basic pattern in which `collect()` is used is to turn one + /// collection into another. You take a collection, call [`iter`] on it, + /// do a bunch of transformations, and then `collect()` at the end. + /// + /// `collect()` can also create instances of types that are not typical + /// collections. + /// + /// Because `collect()` is so general, it can cause problems with type + /// inference. As such, `collect()` is one of the few times you'll see + /// the syntax affectionately known as the 'turbofish': `::<>`. This + /// helps the inference algorithm understand specifically which collection + /// you're trying to collect into. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// let doubled: Array = array![1, 2, 3].into_iter().map(|x| x * 2).collect(); + /// + /// assert_eq!(array![2, 4, 6], doubled); + /// ``` + /// + /// Note that we needed the `: Array` on the left-hand side. + /// + /// Using the 'turbofish' instead of annotating `doubled`: + /// + /// ``` + /// let doubled = array![1, 2, 3].into_iter().map(|x| x * 2).collect::>(); + /// + /// assert_eq!(array![2, 4, 6], doubled); + /// ``` + /// + /// Because `collect()` only cares about what you're collecting into, you can + /// still use a partial type hint, `_`, with the turbofish: + /// + /// ``` + /// let doubled = array![1, 2, 3].into_iter().map(|x| x * 2).collect::>(); + /// + /// assert_eq!(array![2, 4, 6], doubled); + /// ``` + #[inline] + #[must_use] + fn collect, +Destruct>( + self: T, + ) -> B { + FromIterator::::from_iter::(self) + } } diff --git a/corelib/src/test/iter_test.cairo b/corelib/src/test/iter_test.cairo index 605472e239c..3b774bc6223 100644 --- a/corelib/src/test/iter_test.cairo +++ b/corelib/src/test/iter_test.cairo @@ -53,3 +53,8 @@ fn test_iter_adapter_fold() { assert_eq!(sum, 6); } + +#[test] +fn test_iter_adapter_collect() { + assert_eq!((0..3_u32).into_iter().collect(), array![0, 1, 2]); +}