-
Notifications
You must be signed in to change notification settings - Fork 14k
Replace range function with range notation #20909
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
Closed
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ffc5f1c
Added src/doc/grammar.md to hold Rust grammar
icorderi ab24ffe
Copied all the grammar productions from reference.md to grammar.md
icorderi 24904d5
Replace range function with range notation
jatinn 55ebf6e
Update looping.md
jatinn 2303122
Update looping.md
jatinn 602a7d1
update mit-license and copyright
fay-jai 1347825
Update looping.md
jatinn 59d2fda
rebase
a0f86de
Auto merge of #19353 - icorderi:docs/grammar, r=steveklabnik
bors 583c5c5
Auto merge of #20919 - fay-jai:update, r=brson
bors 29bd9a0
Auto merge of #21439 - alexcrichton:rollup, r=alexcrichton
bors 6b26d62
Replace range function with range notation
jatinn 7be4fbd
Update looping.md
jatinn 87bed33
Update looping.md
jatinn 54824ce
Merge branch 'master' of https://github.com/jatinn/rust
jatinn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ for (x = 0; x < 10; x++) { | |
| Instead, it looks like this: | ||
|
|
||
| ```{rust} | ||
| for x in range(0, 10) { | ||
| for x in (0..10) { | ||
| println!("{}", x); // x: i32 | ||
| } | ||
| ``` | ||
|
|
@@ -38,7 +38,7 @@ valid for the loop body. Once the body is over, the next value is fetched from | |
| the iterator, and we loop another time. When there are no more values, the | ||
| `for` loop is over. | ||
|
|
||
| In our example, `range` is a function that takes a start and an end position, | ||
| In our example, the range notation `(0..10)` takes a start and an end position, | ||
| and gives an iterator over those values. The upper bound is exclusive, though, | ||
| so our loop will print `0` through `9`, not `10`. | ||
|
|
||
|
|
@@ -54,8 +54,8 @@ The other kind of looping construct in Rust is the `while` loop. It looks like | |
| this: | ||
|
|
||
| ```{rust} | ||
| let mut x = 5u; // mut x: uint | ||
| let mut done = false; // mut done: bool | ||
| let mut x = 5; | ||
|
||
| let mut done = false; | ||
|
|
||
| while !done { | ||
| x += x - 3; | ||
|
|
@@ -91,7 +91,7 @@ can do with safety and code generation, so you should always prefer | |
| Let's take a look at that `while` loop we had earlier: | ||
|
|
||
| ```{rust} | ||
| let mut x = 5u; | ||
| let mut x = 5; | ||
| let mut done = false; | ||
|
|
||
| while !done { | ||
|
|
@@ -108,7 +108,7 @@ modifying iteration: `break` and `continue`. | |
| In this case, we can write the loop in a better way with `break`: | ||
|
|
||
| ```{rust} | ||
| let mut x = 5u; | ||
| let mut x = 5; | ||
|
|
||
| loop { | ||
| x += x - 3; | ||
|
|
@@ -123,7 +123,7 @@ We now loop forever with `loop` and use `break` to break out early. | |
| iteration. This will only print the odd numbers: | ||
|
|
||
| ```{rust} | ||
| for x in range(0, 10) { | ||
| for x in (0..10) { | ||
| if x % 2 == 0 { continue; } | ||
|
|
||
| println!("{}", x); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 parentheses around
0..10here are not needed. (Same for line 126 below.)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 did not know that.
However not sure about removing the parens mainly as it allows for easier edits.
The second example
could be refactored to
where parens become mandatory right?
Keeping the parens saves you from jumping to the begining to add them later.
However if there is a style guide/convention I am breaking let me know and I will make the edit
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.
AFAIK there is no style guide for range literals yet, as they are quite a recent addition. Still, I think that in cases like this, parentheses are just unnecessary noise. The same argument that parentheses make adding methods calls easier could apply to basically any binary operator…
…but I think that everyone will agree that it’s bad style to put parentheses around every addition operation, and I think this applies to all other operations as well (and thus
..). Also, I don't normally call iterator methods on range iterators because they’re so simple, so I think the case of writing something like(a..b).filter(...)is rare enough anyway to justify a style decision to require parentheses unconditionally.I also think that because the people who are reading The Rust Programming Language will probably be learning the range notation for the first time, it’s best to show that parentheses are not needed, rather than making them think that they are by using them everywhere.
(Also, if you do end up removing the parens, I just realised you’ll need to update line 41 as well as 21 and 126.)
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 you make a valid point