@@ -4010,8 +4010,8 @@ syntax.
4010
4010
4011
4011
# Closures
4012
4012
4013
- So far, we've made lots of functions in Rust. But we've given them all names.
4014
- Rust also allows us to create anonymous functions too . Rust's anonymous
4013
+ So far, we've made lots of functions in Rust, but we've given them all names.
4014
+ Rust also allows us to create anonymous functions. Rust's anonymous
4015
4015
functions are called ** closure** s. By themselves, closures aren't all that
4016
4016
interesting, but when you combine them with functions that take closures as
4017
4017
arguments, really powerful things are possible.
@@ -4040,7 +4040,7 @@ don't need to declare one. This is different from named functions, which
4040
4040
default to returning unit (` () ` ).
4041
4041
4042
4042
There's one big difference between a closure and named functions, and it's in
4043
- the name: a closure "closes over its environment." What's that mean? It means
4043
+ the name: a closure "closes over its environment." What does that mean? It means
4044
4044
this:
4045
4045
4046
4046
``` {rust}
@@ -4056,8 +4056,8 @@ fn main() {
4056
4056
The ` || ` syntax means this is an anonymous closure that takes no arguments.
4057
4057
Without it, we'd just have a block of code in ` {} ` s.
4058
4058
4059
- In other words, a closure has access to variables in the scope that it's
4060
- defined. The closure borrows any variables that it uses. This will error:
4059
+ In other words, a closure has access to variables in the scope where it's
4060
+ defined. The closure borrows any variables it uses, so this will error:
4061
4061
4062
4062
``` {rust,ignore}
4063
4063
fn main() {
@@ -4081,7 +4081,7 @@ let p = proc() { x * x };
4081
4081
println!("{}", p()); // prints 25
4082
4082
```
4083
4083
4084
- Procs have a big difference from closures: they may only be called once. This
4084
+ There is a big difference between procs and closures: procs may only be called once. This
4085
4085
will error when we try to compile:
4086
4086
4087
4087
``` {rust,ignore}
@@ -4174,10 +4174,10 @@ before. And we pass in our `x` argument to each one. Hence 'twice.'
4174
4174
If you do the math, ` (5 * 5) + (5 * 5) == 50 ` , so that's the output we get.
4175
4175
4176
4176
Play around with this concept until you're comfortable with it. Rust's standard
4177
- library uses lots of closures, where appropriate, so you'll be using
4177
+ library uses lots of closures where appropriate, so you'll be using
4178
4178
this technique a lot.
4179
4179
4180
- If we didn't want to give ` square ` a name, we could also just define it inline.
4180
+ If we didn't want to give ` square ` a name, we could just define it inline.
4181
4181
This example is the same as the previous one:
4182
4182
4183
4183
``` {rust}
@@ -4205,12 +4205,12 @@ fn main() {
4205
4205
}
4206
4206
```
4207
4207
4208
- Doing this is not particularly common, but every once in a while, it's useful .
4208
+ Doing this is not particularly common, but it's useful every once in a while.
4209
4209
4210
4210
That's all you need to get the hang of closures! Closures are a little bit
4211
- strange at first, but once you're used to using them, you'll miss them in any
4212
- language that doesn't have them . Passing functions to other functions is
4213
- incredibly powerful. Next, let's look at one of those things : iterators.
4211
+ strange at first, but once you're used to them, you'll miss them
4212
+ in other languages . Passing functions to other functions is
4213
+ incredibly powerful; let's look at one of such situations : iterators.
4214
4214
4215
4215
# Iterators
4216
4216
0 commit comments