Skip to content

Document for-loop in tutorial section on loops #12161

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

Closed
wants to merge 2 commits into from

Conversation

milibopp
Copy link
Contributor

I just started learning Rust and the absence of an explanation of the for-loop in the beginning really bugged me about the tutorial. Hence I simply added these lines, where I would have expected them. I know that there is something later on in the section on traits. However, this simple iteration scheme feels like something that you should be aware of right away.

@@ -582,6 +582,18 @@ loop {
This code prints out a weird sequence of numbers and stops as soon as
it finds one that can be divided by five.

There is also a for-loop that can be used to iterate over a range of numbers
(or, more generally, anything implementing the Iterator trait):
Copy link
Member

Choose a reason for hiding this comment

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

There should be backquotes around Iterator so that its rendered in a code-font

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. I amended the commit to keep the history clean.

@thestinger
Copy link
Contributor

At the moment, for loops are documented as part of the iterator tutorial. I don't think introducing the for loop in isolation is enough because you need to be exposed to the iter methods on containers too, or it's just tempting you to try for x in container { } and hit an obscure error message.

This may be something we should cover in the main tutorial, but I don't think we should have a section on for loops alone, and I don't think we should have coverage in the tutorial, the iterator module and a separate guide that's apparently not discoverable.

@milibopp
Copy link
Contributor Author

Well, it is discoverable, I figured it out pretty quickly. (I did try the container, too, of course...) I sort of just expect a tutorial to be self-contained. And I wondered about how to do that while reading this section immediately. And I suspect many people with a background in other languages will wonder about that. It is somewhat less than optimal to sort of imply traits and iterators in that context, for sure. But then again, the tutorial does that on multiple occasion as far as I remember.

btw, I'm coming mainly from a Python background, where for loops are introduced right away without explaining the iterator protocol. However, iterators and collections are mostly interchangable there, which appears to be not the case for Rust (yet?).

@pnkfelix
Copy link
Member

@thestinger what if, in addition to the range example provided in the current commit, we also provided a concrete example of a for loop that needs to call a method on a collection?

Its too early in the tutorial to bring in vec, so I was thinking maybe something like this:

There is also a for loop that can be used to iterate over a series of values. One such series is a range of numbers:

for n in range(0, 5) {
   println!("{}", n);
}

The snippet above prints integer numbers under 5 starting at 0.

More generally, a for loop works with anything implementing the Iterator trait. Data structures can provide one or more methods that return iterators over their contents. For example, strings support iteration over their contents in various ways:

let s = "Hello";
for c in s.chars() {
    println!("{}", c);
}

The snippet above prints the characters in "Hello" vertically, adding a new line after each character.

And then arguably we might also add a line to the "Vectors and strings" section mentioning the vec methods iter, split and the str methods bytes, chars, words, nfd_chars, ending the non-exhaustive lists of methods with some text like "and various others" (so that one gets a taste of why using methods makes sense, without spelling it out as such).

To me something like the above still seems somewhat lightweight, so that it doesn't need its own section, but it hits the main concerns that you are raising, I think.

@milibopp
Copy link
Contributor Author

Makes sense. Should I extend the PR by that?

@pnkfelix
Copy link
Member

@aepsil0n I do not object to extending the PR in that fashion. It remains to be seen if @thestinger would be satisfied by such a change, but I guess it will be easier for thestinger to judge it if it were part of the PR.

@milibopp
Copy link
Contributor Author

I integrated your suggestion. If this is settled, will it have to be rebased again?


The snippet above prints integer numbers under 5 starting at 0.

More generally, a for loop works with anything implementing the Iterator trait.
Copy link
Member

Choose a reason for hiding this comment

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

(needs back-quotes around Iterator. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

o.O I seem to be unable to learn… amended it again ;)

 As suggested by @pnkfelix in rust-lang#12161, this extends the examples by a for-loop
 that iterates over a string as an example for container iteration.
bors added a commit that referenced this pull request Feb 20, 2014
I just started learning Rust and the absence of an explanation of the for-loop in the beginning really bugged me about the tutorial. Hence I simply added these lines, where I would have expected them. I know that there is something later on in the section on traits. However, this simple iteration scheme feels like something that you should be aware of right away.
@bors bors closed this Feb 20, 2014
@milibopp milibopp deleted the docs/for-loop branch February 20, 2014 19:58
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 25, 2022
internal: Lift out item list path completions from (un)qualified_path

cc rust-lang/rust-analyzer#12144
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.

5 participants