Skip to content

Commit

Permalink
fix: iter::map doc and minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed Jan 4, 2025
1 parent bde6cf1 commit 66be2c6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
17 changes: 7 additions & 10 deletions corelib/src/iter.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
//!
Expand Down Expand Up @@ -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}");
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion corelib/src/option.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,10 @@ pub trait OptionTrait<T> {
self: Option<T>, f: F,
) -> T;

/////////////////////////////////////////////////////////////////////////
// Transforming contained values
/////////////////////////////////////////////////////////////////////////

/// Maps an `Option<T>` to `Option<U>` by applying a function to a contained value (if `Some`)
/// or returns `Option::None` (if `None`).
///
Expand Down Expand Up @@ -718,7 +722,7 @@ pub impl OptionTraitImpl<T> of OptionTrait<T> {
}

#[inline]
fn map<U, F, +Drop<F>, +core::ops::FnOnce<F, (T,)>[Output: U]>(
fn map<U, F, +Destruct<F>, +core::ops::FnOnce<F, (T,)>[Output: U]>(
self: Option<T>, f: F,
) -> Option<U> {
match self {
Expand Down
2 changes: 1 addition & 1 deletion corelib/src/test/iter_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 66be2c6

Please sign in to comment.