@@ -4061,8 +4061,8 @@ syntax.
4061
4061
4062
4062
# Closures
4063
4063
4064
- So far, we've made lots of functions in Rust. But we've given them all names.
4065
- Rust also allows us to create anonymous functions too . Rust's anonymous
4064
+ So far, we've made lots of functions in Rust, but we've given them all names.
4065
+ Rust also allows us to create anonymous functions. Rust's anonymous
4066
4066
functions are called ** closure** s. By themselves, closures aren't all that
4067
4067
interesting, but when you combine them with functions that take closures as
4068
4068
arguments, really powerful things are possible.
@@ -4091,7 +4091,7 @@ don't need to declare one. This is different from named functions, which
4091
4091
default to returning unit (` () ` ).
4092
4092
4093
4093
There's one big difference between a closure and named functions, and it's in
4094
- the name: a closure "closes over its environment." What's that mean? It means
4094
+ the name: a closure "closes over its environment." What does that mean? It means
4095
4095
this:
4096
4096
4097
4097
``` {rust}
@@ -4107,8 +4107,8 @@ fn main() {
4107
4107
The ` || ` syntax means this is an anonymous closure that takes no arguments.
4108
4108
Without it, we'd just have a block of code in ` {} ` s.
4109
4109
4110
- In other words, a closure has access to variables in the scope that it's
4111
- defined. The closure borrows any variables that it uses. This will error:
4110
+ In other words, a closure has access to variables in the scope where it's
4111
+ defined. The closure borrows any variables it uses, so this will error:
4112
4112
4113
4113
``` {rust,ignore}
4114
4114
fn main() {
@@ -4132,7 +4132,7 @@ let p = proc() { x * x };
4132
4132
println!("{}", p()); // prints 25
4133
4133
```
4134
4134
4135
- Procs have a big difference from closures: they may only be called once. This
4135
+ There is a big difference between procs and closures: procs may only be called once. This
4136
4136
will error when we try to compile:
4137
4137
4138
4138
``` {rust,ignore}
@@ -4225,10 +4225,10 @@ before. And we pass in our `x` argument to each one. Hence 'twice.'
4225
4225
If you do the math, ` (5 * 5) + (5 * 5) == 50 ` , so that's the output we get.
4226
4226
4227
4227
Play around with this concept until you're comfortable with it. Rust's standard
4228
- library uses lots of closures, where appropriate, so you'll be using
4228
+ library uses lots of closures where appropriate, so you'll be using
4229
4229
this technique a lot.
4230
4230
4231
- If we didn't want to give ` square ` a name, we could also just define it inline.
4231
+ If we didn't want to give ` square ` a name, we could just define it inline.
4232
4232
This example is the same as the previous one:
4233
4233
4234
4234
``` {rust}
@@ -4256,12 +4256,12 @@ fn main() {
4256
4256
}
4257
4257
```
4258
4258
4259
- Doing this is not particularly common, but every once in a while, it's useful .
4259
+ Doing this is not particularly common, but it's useful every once in a while.
4260
4260
4261
4261
That's all you need to get the hang of closures! Closures are a little bit
4262
- strange at first, but once you're used to using them, you'll miss them in any
4263
- language that doesn't have them . Passing functions to other functions is
4264
- incredibly powerful. Next, let's look at one of those things: iterators.
4262
+ strange at first, but once you're used to them, you'll miss them
4263
+ in other languages . Passing functions to other functions is
4264
+ incredibly powerful, as you will see in the following chapter about iterators.
4265
4265
4266
4266
# Iterators
4267
4267
0 commit comments