@@ -4467,18 +4467,19 @@ see why consumers matter.
4467
4467
4468
4468
## Iterators
4469
4469
4470
- As we've said before, an iterator is something that we can call the ` .next() `
4471
- method on repeatedly, and it gives us a sequence of things. Because you need
4472
- to call the method, this means that iterators are ** lazy** . This code, for
4473
- example, does not actually generate the numbers ` 1-100 ` , and just creates a
4474
- value that represents the sequence:
4470
+ As we've said before, an iterator is something that we can call the
4471
+ ` .next() ` method on repeatedly, and it gives us a sequence of things.
4472
+ Because you need to call the method, this means that iterators
4473
+ are ** lazy** and don't need to generate all of the values upfront.
4474
+ This code, for example, does not actually generate the numbers
4475
+ ` 1-100 ` , and just creates a value that represents the sequence:
4475
4476
4476
4477
``` {rust}
4477
4478
let nums = range(1i, 100i);
4478
4479
```
4479
4480
4480
4481
Since we didn't do anything with the range, it didn't generate the sequence.
4481
- Once we add the consumer:
4482
+ Let's add the consumer:
4482
4483
4483
4484
``` {rust}
4484
4485
let nums = range(1i, 100i).collect::<Vec<int>>();
@@ -4507,8 +4508,8 @@ std::iter::count(1i, 5i);
4507
4508
```
4508
4509
4509
4510
This iterator counts up from one, adding five each time. It will give
4510
- you a new integer every time, forever. Well , technically, until the
4511
- maximum number that an ` int ` can represent . But since iterators are lazy,
4511
+ you a new integer every time, forever (well , technically, until it reaches the
4512
+ maximum number representable by an ` int ` ) . But since iterators are lazy,
4512
4513
that's okay! You probably don't want to use ` collect() ` on it, though...
4513
4514
4514
4515
That's enough about iterators. Iterator adapters are the last concept
@@ -5251,8 +5252,8 @@ to do something that it can't currently do? You may be able to write a macro
5251
5252
to extend Rust's capabilities.
5252
5253
5253
5254
You've already used one macro extensively: ` println! ` . When we invoke
5254
- a Rust macro, we need to use the exclamation mark (` ! ` ). There's two reasons
5255
- that this is true : the first is that it makes it clear when you're using a
5255
+ a Rust macro, we need to use the exclamation mark (` ! ` ). There are two reasons
5256
+ why this is so : the first is that it makes it clear when you're using a
5256
5257
macro. The second is that macros allow for flexible syntax, and so Rust must
5257
5258
be able to tell where a macro starts and ends. The ` !(...) ` helps with this.
5258
5259
@@ -5267,7 +5268,7 @@ println!("x is: {}", x);
5267
5268
5268
5269
The ` println! ` macro does a few things:
5269
5270
5270
- 1 . It parses the string to find any ` {} ` s
5271
+ 1 . It parses the string to find any ` {} ` s.
5271
5272
2 . It checks that the number of ` {} ` s matches the number of other arguments.
5272
5273
3 . It generates a bunch of Rust code, taking this in mind.
5273
5274
@@ -5276,8 +5277,8 @@ Rust will generate code that takes all of the types into account. If
5276
5277
` println! ` was a function, it could still do this type checking, but it
5277
5278
would happen at run time rather than compile time.
5278
5279
5279
- We can check this out using a special flag to ` rustc ` . This code, in a file
5280
- ` print.rs ` :
5280
+ We can check this out using a special flag to ` rustc ` . Put this code in a file
5281
+ called ` print.rs ` :
5281
5282
5282
5283
``` {rust}
5283
5284
fn main() {
@@ -5286,7 +5287,7 @@ fn main() {
5286
5287
}
5287
5288
```
5288
5289
5289
- Can have its macros expanded like this: ` rustc print.rs --pretty=expanded ` , will
5290
+ You can have the macros expanded like this: ` rustc print.rs --pretty=expanded ` – which will
5290
5291
give us this huge result:
5291
5292
5292
5293
``` {rust,ignore}
@@ -5325,12 +5326,12 @@ invoke the `println_args` function with the generated arguments.
5325
5326
This is the code that Rust actually compiles. You can see all of the extra
5326
5327
information that's here. We get all of the type safety and options that it
5327
5328
provides, but at compile time, and without needing to type all of this out.
5328
- This is how macros are powerful. Without them, you would need to type all of
5329
- this by hand to get a type checked ` println ` .
5329
+ This is how macros are powerful: without them you would need to type all of
5330
+ this by hand to get a type- checked ` println ` .
5330
5331
5331
5332
For more on macros, please consult [ the Macros Guide] ( guide-macros.html ) .
5332
- Macros are a very advanced and still slightly experimental feature, but don't
5333
- require a deep understanding to call , since they look just like functions. The
5333
+ Macros are a very advanced and still slightly experimental feature, but they don't
5334
+ require a deep understanding to be called , since they look just like functions. The
5334
5335
Guide can help you if you want to write your own.
5335
5336
5336
5337
# Unsafe
@@ -5347,8 +5348,8 @@ keyword, which indicates that the function may not behave properly.
5347
5348
5348
5349
Second, if you'd like to create some sort of shared-memory data structure, Rust
5349
5350
won't allow it, because memory must be owned by a single owner. However, if
5350
- you're planning on making access to that shared memory safe, such as with a
5351
- mutex, _ you_ know that it's safe, but Rust can't know. Writing an ` unsafe `
5351
+ you're planning on making access to that shared memory safe – such as with a
5352
+ mutex – _ you_ know that it's safe, but Rust can't know. Writing an ` unsafe `
5352
5353
block allows you to ask the compiler to trust you. In this case, the _ internal_
5353
5354
implementation of the mutex is considered unsafe, but the _ external_ interface
5354
5355
we present is safe. This allows it to be effectively used in normal Rust, while
0 commit comments