Skip to content

Commit f174bca

Browse files
committed
Auto merge of #21834 - genbattle:doc-range-notation, r=steveklabnik
Replaced outdated use of the `range(start, end)` function where appropriate with `start..end`, and tweaked the examples to compile and run with the latest rust. I also fixed two periphery compile issues in reference.md which were occluding whether there were any new errors created by these changes.
2 parents b8c112f + 8300095 commit f174bca

File tree

6 files changed

+49
-49
lines changed

6 files changed

+49
-49
lines changed

src/doc/reference.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3005,7 +3005,7 @@ Some examples of call expressions:
30053005
# fn add(x: i32, y: i32) -> i32 { 0 }
30063006
30073007
let x: i32 = add(1i32, 2i32);
3008-
let pi: Option<f32> = "3.14".parse().ok();
3008+
let pi: Result<f32, _> = "3.14".parse();
30093009
```
30103010

30113011
### Lambda expressions
@@ -3148,7 +3148,7 @@ An example of a for loop over a series of integers:
31483148

31493149
```
31503150
# fn bar(b:usize) { }
3151-
for i in range(0us, 256) {
3151+
for i in 0us..256 {
31523152
bar(i);
31533153
}
31543154
```
@@ -3532,7 +3532,7 @@ An example of each kind:
35323532
```{rust}
35333533
let vec: Vec<i32> = vec![1, 2, 3];
35343534
let arr: [i32; 3] = [1, 2, 3];
3535-
let s: &[i32] = &vec;
3535+
let s: &[i32] = &vec[];
35363536
```
35373537

35383538
As you can see, the `vec!` macro allows you to create a `Vec<T>` easily. The

src/doc/trpl/concurrency.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ use std::sync::mpsc;
354354
fn main() {
355355
let (tx, rx) = mpsc::channel();
356356
357-
for _ in range(0, 10) {
357+
for _ in 0..10 {
358358
let tx = tx.clone();
359359
360360
Thread::spawn(move || {

src/doc/trpl/guessing-game.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ a function for that:
400400
let input = old_io::stdin().read_line()
401401
.ok()
402402
.expect("Failed to read line");
403-
let input_num: Option<u32> = input.parse().ok();
403+
let input_num: Result<u32, _> = input.parse();
404404
```
405405
406406
The `parse` function takes in a `&str` value and converts it into something.
@@ -422,8 +422,8 @@ In this case, we say `x` is a `u32` explicitly, so Rust is able to properly
422422
tell `random()` what to generate. In a similar fashion, both of these work:
423423
424424
```{rust,ignore}
425-
let input_num = "5".parse::<u32>().ok(); // input_num: Option<u32>
426-
let input_num: Option<u32> = "5".parse().ok(); // input_num: Option<u32>
425+
let input_num = "5".parse::<u32>(); // input_num: Option<u32>
426+
let input_num: Result<u32, _> = "5".parse(); // input_num: Result<u32, <u32 as FromStr>::Err>
427427
```
428428
429429
Here we're converting the `Result` returned by `parse` to an `Option` by using
@@ -447,9 +447,9 @@ fn main() {
447447
let input = old_io::stdin().read_line()
448448
.ok()
449449
.expect("Failed to read line");
450-
let input_num: Option<u32> = input.parse().ok();
450+
let input_num: Result<u32, _> = input.parse();
451451
452-
println!("You guessed: {}", input_num);
452+
println!("You guessed: {:?}", input_num);
453453
454454
match cmp(input_num, secret_number) {
455455
Ordering::Less => println!("Too small!"),
@@ -497,11 +497,11 @@ fn main() {
497497
let input = old_io::stdin().read_line()
498498
.ok()
499499
.expect("Failed to read line");
500-
let input_num: Option<u32> = input.parse().ok();
500+
let input_num: Result<u32, _> = input.parse();
501501

502502
let num = match input_num {
503-
Some(num) => num,
504-
None => {
503+
Ok(num) => num,
504+
Err(_) => {
505505
println!("Please input a number!");
506506
return;
507507
}
@@ -564,11 +564,11 @@ fn main() {
564564
let input = old_io::stdin().read_line()
565565
.ok()
566566
.expect("Failed to read line");
567-
let input_num: Option<u32> = input.trim().parse().ok();
567+
let input_num: Result<u32, _> = input.trim().parse();
568568

569569
let num = match input_num {
570-
Some(num) => num,
571-
None => {
570+
Ok(num) => num,
571+
Err(_) => {
572572
println!("Please input a number!");
573573
return;
574574
}
@@ -640,11 +640,11 @@ fn main() {
640640
let input = old_io::stdin().read_line()
641641
.ok()
642642
.expect("Failed to read line");
643-
let input_num: Option<u32> = input.trim().parse().ok();
643+
let input_num: Result<u32, _> = input.trim().parse();
644644
645645
let num = match input_num {
646-
Some(num) => num,
647-
None => {
646+
Ok(num) => num,
647+
Err(_) => {
648648
println!("Please input a number!");
649649
return;
650650
}
@@ -716,11 +716,11 @@ fn main() {
716716
let input = old_io::stdin().read_line()
717717
.ok()
718718
.expect("Failed to read line");
719-
let input_num: Option<u32> = input.trim().parse().ok();
719+
let input_num: Result<u32, _> = input.trim().parse();
720720

721721
let num = match input_num {
722-
Some(num) => num,
723-
None => {
722+
Ok(num) => num,
723+
Err(_) => {
724724
println!("Please input a number!");
725725
return;
726726
}
@@ -772,11 +772,11 @@ fn main() {
772772
let input = old_io::stdin().read_line()
773773
.ok()
774774
.expect("Failed to read line");
775-
let input_num: Option<u32> = input.trim().parse().ok();
775+
let input_num: Result<u32, _> = input.trim().parse();
776776

777777
let num = match input_num {
778-
Some(num) => num,
779-
None => {
778+
Ok(num) => num,
779+
Err(_) => {
780780
println!("Please input a number!");
781781
continue;
782782
}
@@ -849,11 +849,11 @@ fn main() {
849849
let input = old_io::stdin().read_line()
850850
.ok()
851851
.expect("Failed to read line");
852-
let input_num: Option<u32> = input.trim().parse().ok();
852+
let input_num: Result<u32, _> = input.trim().parse();
853853

854854
let num = match input_num {
855-
Some(num) => num,
856-
None => {
855+
Ok(num) => num,
856+
Err(_) => {
857857
println!("Please input a number!");
858858
continue;
859859
}

src/doc/trpl/iterators.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Let's talk about loops.
55
Remember Rust's `for` loop? Here's an example:
66

77
```{rust}
8-
for x in range(0, 10) {
8+
for x in 0..10 {
99
println!("{}", x);
1010
}
1111
```
@@ -17,7 +17,7 @@ call the `.next()` method on repeatedly, and it gives us a sequence of things.
1717
Like this:
1818

1919
```{rust}
20-
let mut range = range(0, 10);
20+
let mut range = 0..10;
2121
2222
loop {
2323
match range.next() {
@@ -52,7 +52,7 @@ a vector, you may be tempted to write this:
5252
```{rust}
5353
let nums = vec![1, 2, 3];
5454
55-
for i in range(0, nums.len()) {
55+
for i in 0..nums.len() {
5656
println!("{}", nums[i]);
5757
}
5858
```
@@ -118,7 +118,7 @@ The most common consumer is `collect()`. This code doesn't quite compile,
118118
but it shows the intention:
119119

120120
```{rust,ignore}
121-
let one_to_one_hundred = range(1, 101).collect();
121+
let one_to_one_hundred = (1..101i32).collect();
122122
```
123123

124124
As you can see, we call `collect()` on our iterator. `collect()` takes
@@ -128,7 +128,7 @@ type of things you want to collect, and so you need to let it know.
128128
Here's the version that does compile:
129129

130130
```{rust}
131-
let one_to_one_hundred = range(1, 101).collect::<Vec<i32>>();
131+
let one_to_one_hundred = (1..101i32).collect::<Vec<i32>>();
132132
```
133133

134134
If you remember, the `::<>` syntax allows us to give a type hint,
@@ -138,7 +138,7 @@ and so we tell it that we want a vector of integers.
138138
is one:
139139

140140
```{rust}
141-
let greater_than_forty_two = range(0, 100)
141+
let greater_than_forty_two = (0..100i32)
142142
.find(|x| *x > 42);
143143
144144
match greater_than_forty_two {
@@ -155,7 +155,7 @@ element, `find` returns an `Option` rather than the element itself.
155155
Another important consumer is `fold`. Here's what it looks like:
156156

157157
```{rust}
158-
let sum = range(1, 4)
158+
let sum = (1..4)
159159
.fold(0, |sum, x| sum + x);
160160
```
161161

@@ -179,7 +179,7 @@ in this iterator:
179179
We called `fold()` with these arguments:
180180

181181
```{rust}
182-
# range(1, 4)
182+
# (1..4)
183183
.fold(0, |sum, x| sum + x);
184184
```
185185

@@ -210,20 +210,20 @@ This code, for example, does not actually generate the numbers
210210
`1-100`, and just creates a value that represents the sequence:
211211

212212
```{rust}
213-
let nums = range(1, 100);
213+
let nums = 1..100;
214214
```
215215

216216
Since we didn't do anything with the range, it didn't generate the sequence.
217217
Let's add the consumer:
218218

219219
```{rust}
220-
let nums = range(1, 100).collect::<Vec<i32>>();
220+
let nums = (1..100).collect::<Vec<i32>>();
221221
```
222222

223-
Now, `collect()` will require that `range()` give it some numbers, and so
223+
Now, `collect()` will require that the range gives it some numbers, and so
224224
it will do the work of generating the sequence.
225225

226-
`range` is one of two basic iterators that you'll see. The other is `iter()`,
226+
A range is one of two basic iterators that you'll see. The other is `iter()`,
227227
which you've used before. `iter()` can turn a vector into a simple iterator
228228
that gives you each element in turn:
229229

@@ -256,7 +256,7 @@ we need to talk about with regards to iterators. Let's get to it!
256256
a new iterator. The simplest one is called `map`:
257257

258258
```{rust,ignore}
259-
range(1, 100).map(|x| x + 1);
259+
(1..100i32).map(|x| x + 1);
260260
```
261261

262262
`map` is called upon another iterator, and produces a new iterator where each
@@ -267,15 +267,15 @@ compile the example, you'll get a warning:
267267
```{notrust,ignore}
268268
warning: unused result which must be used: iterator adaptors are lazy and
269269
do nothing unless consumed, #[warn(unused_must_use)] on by default
270-
range(1, 100).map(|x| x + 1);
270+
(1..100).map(|x| x + 1);
271271
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
272272
```
273273

274274
Laziness strikes again! That closure will never execute. This example
275275
doesn't print any numbers:
276276

277277
```{rust,ignore}
278-
range(1, 100).map(|x| println!("{}", x));
278+
(1..100).map(|x| println!("{}", x));
279279
```
280280

281281
If you are trying to execute a closure on an iterator for its side effects,
@@ -307,7 +307,7 @@ returns `true` or `false`. The new iterator `filter()` produces
307307
only the elements that that closure returns `true` for:
308308

309309
```{rust}
310-
for i in range(1, 100).filter(|&x| x % 2 == 0) {
310+
for i in (1..100i32).filter(|&x| x % 2 == 0) {
311311
println!("{}", i);
312312
}
313313
```
@@ -322,7 +322,7 @@ You can chain all three things together: start with an iterator, adapt it
322322
a few times, and then consume the result. Check it out:
323323

324324
```{rust}
325-
range(1, 1000)
325+
(1..1000i32)
326326
.filter(|&x| x % 2 == 0)
327327
.filter(|&x| x % 3 == 0)
328328
.take(5)

src/doc/trpl/ownership.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ struct Wheel {
418418
fn main() {
419419
let car = Car { name: "DeLorean".to_string() };
420420
421-
for _ in range(0, 4) {
421+
for _ in 0..4 {
422422
Wheel { size: 360, owner: car };
423423
}
424424
}
@@ -456,7 +456,7 @@ fn main() {
456456

457457
let car_owner = Rc::new(car);
458458

459-
for _ in range(0, 4) {
459+
for _ in 0..4 {
460460
Wheel { size: 360, owner: car_owner.clone() };
461461
}
462462
}

src/doc/trpl/testing.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ use test::Bencher;
512512
#[bench]
513513
fn bench_xor_1000_ints(b: &mut Bencher) {
514514
b.iter(|| {
515-
range(0, 1000).fold(0, |old, new| old ^ new);
515+
(0..1000).fold(0, |old, new| old ^ new);
516516
});
517517
}
518518
```
@@ -537,7 +537,7 @@ computation entirely. This could be done for the example above by adjusting the
537537
# impl X { fn iter<T, F>(&self, _: F) where F: FnMut() -> T {} } let b = X;
538538
b.iter(|| {
539539
// note lack of `;` (could also use an explicit `return`).
540-
range(0, 1000).fold(0, |old, new| old ^ new)
540+
(0..1000).fold(0, |old, new| old ^ new)
541541
});
542542
```
543543

@@ -554,7 +554,7 @@ extern crate test;
554554
b.iter(|| {
555555
let n = test::black_box(1000);
556556

557-
range(0, n).fold(0, |a, b| a ^ b)
557+
(0..n).fold(0, |a, b| a ^ b)
558558
})
559559
# }
560560
```

0 commit comments

Comments
 (0)