Skip to content

Document closure to fn coercion feature #61

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
May 31, 2017
Merged
Changes from 1 commit
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
17 changes: 15 additions & 2 deletions src/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,22 @@ more of the closure traits:
* `Fn`
: The closure can be called multiple times through a shared reference.
A closure called as `Fn` can neither move out from nor mutate values
from its environment. `Fn` inherits from `FnMut`, which itself
inherits from `FnOnce`.
from its environment, but read-only access to such values is allowed.
`Fn` inherits from `FnMut`, which itself inherits from `FnOnce`.

Closures that don't use anything from their environment ("non capturing closures")
can be coerced to function pointers (`fn`) with the matching signature.
To adopt the example from the section above:

```rust
let add = |x, y| x + y;

let mut x = add(5,7);

type Binop = fn(i32, i32) -> i32;
let bo: Binop = add;
x = bo(5,7);
```

## Trait objects

Expand Down