Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guide: iterators #16408

Merged
merged 1 commit into from
Aug 20, 2014
Merged

Guide: iterators #16408

merged 1 commit into from
Aug 20, 2014

Conversation

steveklabnik
Copy link
Member

An introduction to iterators. I kinda like this, but I kinda don't. Hmmm.

result for our sum. `1 + 2 + 3 = 6`, and that's the result we got.

Whew. `fold` can be a bit strange the first few times you see it, but once it
clicks, you an use it all over the place. Any time you have a list of things,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/an/can/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!

@killercup
Copy link
Member

Nicely written. I expected to learn two more things though:

  • Iterators themselves are lazy
  • One can simply implement the Iterator trait by writing a next method that returns Some(next_value) as long as there is stuff to iterate and None at the end.

(Correct me if this is wrong.)

@steveklabnik
Copy link
Member Author

@killercup great point on the lazy, but we haven't actually introduced traits yet, so we can't talk about that. Implementing iterators is out of the scope of this guide, but should certainly go in the broader iterator documentation.

@steveklabnik
Copy link
Member Author

Okay. New draft up. This is significantly different, but addresses all of the problems with the last one. What do we think?

We make a mutable binding to the return value of `range`, which is our iterator.
We then `loop`, with an inner `match`. This `match` is used on the result of
`range.next()`, which gives us a reference to the next value of the iterator.
`next` returns an `Option<int>`, in this case, which will be `Some(int)` when
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the int in Some(int) meant to be the type or meant to be a name binding?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the type.

@huonw
Copy link
Member

huonw commented Aug 13, 2014

I like it!

```

`fold()` is a consumer that looks like this:
`fold(base, |accumulator, element| result`. It takes two arguments: the first
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing ). Also, looking at it written down, maybe it could be just fold(base, |accumulator, element| ...).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed.

```

This iterator counts up from one, adding five each time. It will give
you a new integer every time, forever. But since iterators are lazy,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be precise, they're not always new as it repeats after 264 (or 232) elements.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a sentence here. Once we address #15526 , I'll make it more specific.

bors added a commit that referenced this pull request Aug 20, 2014
An introduction to iterators. I kinda like this, but I kinda don't. Hmmm.
@bors bors closed this Aug 20, 2014
@bors bors merged commit baf305d into rust-lang:master Aug 20, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants