Skip to content

Guide: Closures: minor wording fixes #18340

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

Merged
merged 2 commits into from
Oct 29, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/doc/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -4010,8 +4010,8 @@ syntax.

# Closures

So far, we've made lots of functions in Rust. But we've given them all names.
Rust also allows us to create anonymous functions too. Rust's anonymous
So far, we've made lots of functions in Rust, but we've given them all names.
Rust also allows us to create anonymous functions. Rust's anonymous
functions are called **closure**s. By themselves, closures aren't all that
interesting, but when you combine them with functions that take closures as
arguments, really powerful things are possible.
Expand Down Expand Up @@ -4040,7 +4040,7 @@ don't need to declare one. This is different from named functions, which
default to returning unit (`()`).

There's one big difference between a closure and named functions, and it's in
the name: a closure "closes over its environment." What's that mean? It means
the name: a closure "closes over its environment." What does that mean? It means
this:

```{rust}
Expand All @@ -4056,8 +4056,8 @@ fn main() {
The `||` syntax means this is an anonymous closure that takes no arguments.
Without it, we'd just have a block of code in `{}`s.

In other words, a closure has access to variables in the scope that it's
defined. The closure borrows any variables that it uses. This will error:
In other words, a closure has access to variables in the scope where it's
defined. The closure borrows any variables it uses, so this will error:

```{rust,ignore}
fn main() {
Expand All @@ -4081,7 +4081,7 @@ let p = proc() { x * x };
println!("{}", p()); // prints 25
```

Procs have a big difference from closures: they may only be called once. This
There is a big difference between procs and closures: procs may only be called once. This
will error when we try to compile:

```{rust,ignore}
Expand Down Expand Up @@ -4174,10 +4174,10 @@ before. And we pass in our `x` argument to each one. Hence 'twice.'
If you do the math, `(5 * 5) + (5 * 5) == 50`, so that's the output we get.

Play around with this concept until you're comfortable with it. Rust's standard
library uses lots of closures, where appropriate, so you'll be using
library uses lots of closures where appropriate, so you'll be using
this technique a lot.

If we didn't want to give `square` a name, we could also just define it inline.
If we didn't want to give `square` a name, we could just define it inline.
This example is the same as the previous one:

```{rust}
Expand Down Expand Up @@ -4205,12 +4205,12 @@ fn main() {
}
```

Doing this is not particularly common, but every once in a while, it's useful.
Doing this is not particularly common, but it's useful every once in a while.

That's all you need to get the hang of closures! Closures are a little bit
strange at first, but once you're used to using them, you'll miss them in any
language that doesn't have them. Passing functions to other functions is
incredibly powerful. Next, let's look at one of those things: iterators.
strange at first, but once you're used to them, you'll miss them
in other languages. Passing functions to other functions is
incredibly powerful, as you will see in the following chapter about iterators.

# Iterators

Expand Down