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 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
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

@@ -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
@@ -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