diff --git a/src/type-coercions.md b/src/type-coercions.md index 6301e5e83..b735d8af4 100644 --- a/src/type-coercions.md +++ b/src/type-coercions.md @@ -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 diff --git a/src/types.md b/src/types.md index 7fd9a0967..e8379f695 100644 --- a/src/types.md +++ b/src/types.md @@ -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