-
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
Changes from 3 commits
ffc5f1c
ab24ffe
24904d5
55ebf6e
2303122
602a7d1
1347825
59d2fda
a0f86de
583c5c5
29bd9a0
6b26d62
7be4fbd
87bed33
54824ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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,7 +54,7 @@ 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 x = 5; // mut x: i32 | ||
|
||
| let mut done = false; // mut done: bool | ||
|
|
||
| while !done { | ||
|
|
@@ -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); | ||
|
|
||
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