Skip to content

Commit

Permalink
Merge pull request rust-lang#61 from est31/master
Browse files Browse the repository at this point in the history
Document closure to fn coercion feature
  • Loading branch information
steveklabnik authored May 31, 2017
2 parents e3479cb + 4c475e0 commit fdaa801
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/type-coercions.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Type coercions

Coercions are defined in [RFC 401]. A coercion is implicit and has no syntax.
Coercions are defined in [RFC 401]. [RFC 1558] then expanded on that.
A coercion is implicit and has no syntax.

[RFC 401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
[RFC 1558]: https://github.com/rust-lang/rfcs/blob/master/text/1558-closure-to-fn-coercion.md

## Coercion sites

Expand Down Expand Up @@ -143,3 +145,5 @@ Coercion is allowed between the following types:
In the future, coerce_inner will be recursively extended to tuples and
structs. In addition, coercions from sub-traits to super-traits will be
added. See [RFC 401] for more details.

* Non capturing closures to `fn` pointers
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

0 comments on commit fdaa801

Please sign in to comment.