-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Conversation
@@ -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): |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
At the moment, This may be something we should cover in the main tutorial, but I don't think we should have a section on |
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?). |
@thestinger what if, in addition to the Its too early in the tutorial to bring in
And then arguably we might also add a line to the "Vectors and strings" section mentioning the 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. |
Makes sense. Should I extend the PR by that? |
@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. |
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. |
There was a problem hiding this comment.
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
. :)
There was a problem hiding this comment.
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.
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.
internal: Lift out item list path completions from (un)qualified_path cc rust-lang/rust-analyzer#12144
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.