From 66be2c66a9376874874186ddc1aa1d038138972e Mon Sep 17 00:00:00 2001 From: julio4 <30329843+julio4@users.noreply.github.com> Date: Sat, 4 Jan 2025 12:14:59 +0100 Subject: [PATCH] fix: iter::map doc and minor fixes --- corelib/src/iter.cairo | 17 +++++++---------- corelib/src/option.cairo | 6 +++++- corelib/src/test/iter_test.cairo | 2 +- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/corelib/src/iter.cairo b/corelib/src/iter.cairo index 852aca83189..64ac551f914 100644 --- a/corelib/src/iter.cairo +++ b/corelib/src/iter.cairo @@ -54,9 +54,9 @@ //! [`Option::Some(Item)`]: Option::Some //! [`next`]: Iterator::next //! -//! # Iterators in Cairo +//! # Forms of iteration //! -//! There are currently one common method which can create iterators from a collection: +//! There is currently only one common method which can create iterators from a collection: //! //! * `into_iter()`, which iterates over `T`. //! @@ -138,19 +138,19 @@ //! ``` //! //! This will print the numbers one through five, each on their own line. But -//! you'll notice something here: we never called anything on our vector to +//! you'll notice something here: we never called anything on our array to //! produce an iterator. What gives? //! -//! There's a trait in the standard library for converting something into an +//! There's a trait in the core library for converting something into an //! iterator: [`IntoIterator`]. This trait has one method, [`into_iter`], //! which converts the thing implementing [`IntoIterator`] into an iterator. //! Let's take a look at that `for` loop again, and what the compiler converts //! it into: //! -//! [`into_iter`]: IntoIterator::into_iter +//! [`into_iter`]: core::iter::IntoIterator::into_iter //! //! ``` -//! let values = vec![1, 2, 3, 4, 5]; +//! let values = array![1, 2, 3, 4, 5]; //! //! for x in values { //! println!("{x}"); @@ -182,7 +182,7 @@ //! that returns, calling [`next`] over and over until we see a `Option::None`. At //! that point, we `break` out of the loop, and we're done iterating. //! -//! There's one more subtle bit here: the standard library contains an +//! There's one more subtle bit here: the core library contains an //! interesting implementation of [`IntoIterator`]: //! //! ```ignore (only-for-syntax-highlight) @@ -205,9 +205,6 @@ //! The only adapter for now is [`map`]. //! For more, see the [`map`] documentation. //! -//! If an iterator adapter panics, the iterator will be in an unspecified (but -//! memory safe) state. -//! //! [`map`]: Iterator::map //! //! # Laziness diff --git a/corelib/src/option.cairo b/corelib/src/option.cairo index 619a875e680..0712514e198 100644 --- a/corelib/src/option.cairo +++ b/corelib/src/option.cairo @@ -508,6 +508,10 @@ pub trait OptionTrait { self: Option, f: F, ) -> T; + ///////////////////////////////////////////////////////////////////////// + // Transforming contained values + ///////////////////////////////////////////////////////////////////////// + /// Maps an `Option` to `Option` by applying a function to a contained value (if `Some`) /// or returns `Option::None` (if `None`). /// @@ -718,7 +722,7 @@ pub impl OptionTraitImpl of OptionTrait { } #[inline] - fn map, +core::ops::FnOnce[Output: U]>( + fn map, +core::ops::FnOnce[Output: U]>( self: Option, f: F, ) -> Option { match self { diff --git a/corelib/src/test/iter_test.cairo b/corelib/src/test/iter_test.cairo index ec6631e3940..80b337da19e 100644 --- a/corelib/src/test/iter_test.cairo +++ b/corelib/src/test/iter_test.cairo @@ -4,7 +4,7 @@ use crate::iter::{IntoIterator, Iterator}; fn test_iter_adapter_map() { let a = array![1, 2, 3]; let mut iter = a.into_iter().map(|x| 2 * x); - + assert_eq!(iter.next(), Option::Some(2)); assert_eq!(iter.next(), Option::Some(4)); assert_eq!(iter.next(), Option::Some(6));