-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
loop_break_value: add documentation for book #41857
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @steveklabnik (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
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.
This is great! Just a few comments.
Documentation to be appended to section 3.6 of the book: Loops (after "Loop Labels", or before if | ||
the "Break" section is moved). If this is deemed too complex a feature this early in the book, it | ||
could also be moved to a new section (please advise). This would allow examples breaking with | ||
non-primitive types, references, and discussion of coercion (probably unnecessary however). |
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.
So, our current strategy is that we have an appendix G, where stuff can land at first https://doc.rust-lang.org/nightly/book/second-edition/appendix-07-newest-features.html
I'd recommend saying this should go there instead
------------------------ | ||
|
||
### Loops as expressions | ||
|
||
Like everything else in Rust, loops are expressions; for example, the following is perfectly legal, |
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.
not everything is an expression; I'd soften this to "most things"
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.
Thanks for writing this, it's very nice, I'm not an expert on docs or anything but left some comments with opinions.
Also I find it a bit confusing that through the text loop means any loop and loop
is used meaning exactly the loop
construct. Perhaps it would be interesting to rephrase using the word loop to mean only the loop
construct?
|
||
For now, breaking with a value is only possible with `loop`; the same functionality may | ||
some day be added to `for` and `while` (this would require some new syntax like | ||
`while f() { break 1; } default { break 0; }`). |
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.
I don't like that this speculates about future language extensions and their syntax.
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.
I half agree with you here, but I think that if there is no mention of the discrepancy between loop
and for
/while
that some readers will assume break-with-value also works there and confused why it doesn't work, and some will start asking the obvious why.
I suppose I could just omit the last bit about why for
and while
don't support break-with-value, but it still leaves an obvious question why.
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.
The Rust book already uses loop to refer to all three repetitive control-flow constructs:
Rust has three kinds of loops:
loop
,while
, andfor
.
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.
Your concerns are valid, but the why response is complex and depends on who you ask, imo it's too opinionated to get into that here. I definitely think your use of the loop term is correct, I just worry it may be confusing in this context.
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.
Yeah; we don't say stuff like this in the docs because it often bitrots; then people think you can't do it when you actually can!
println!("Hello, {}", n); | ||
}; | ||
assert_eq!(result, ()); | ||
``` |
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.
I don't find this example very helpful in understanding the feature. The return value of for
is a nice curiousity but I don't see much didatic value to this.
``` | ||
|
||
Until now, all the loops you have seen evaluate to either `()` or `!`, the latter being special | ||
syntax for "no value", meaning the loop never exits. A `loop` can instead evaluate to |
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.
I don't find the ()
vs !
distinction very useful, the meaning of !
as a type is unstable so maybe it's best to leave it out of this explanation.
### Loops as expressions | ||
|
||
Like most things in Rust, loops are expressions, and have a value; normally `()` unless the loop | ||
never exits. |
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.
Nice, I think this is more to the point now. "unless the loop never exits" only applies to loop
and not any loop that never exists (see how even we are getting confused by the terms), for
and while
always evaluate to ()
. But diverging things are very niche, maybe we should just not mention this here?
Yes, each snippet runs as its own test, so they all need them.
The simplest way is to run
Yes, don't worry about that.
Yup, it's in a separate repo. |
Yep, in each snippet. See, for example, the docs on the
You should be able to run just the unstable book tests by doing
Yep, I've logged this in our spurious failure tracking. Let's see what happens when travis reruns after you fix the tests.
Yep, that looks right! |
AHHHH STEVE BEAT ME |
connected to #37339 |
Thanks Steve, Carols. Tests updated to pass, but I used |
I uh, forgot |
@bors: r+ |
📌 Commit 7ab35b7 has been approved by |
loop_break_value: add documentation for book Some notes at the top of the file. r? @steveklabnik
☀️ Test successful - status-appveyor, status-travis |
Some notes at the top of the file.
r? @steveklabnik