From 1f67a1c26c7d5de53bac7ead7a6f9cf87769e53c Mon Sep 17 00:00:00 2001 From: est31 Date: Tue, 23 May 2017 03:40:15 +0200 Subject: [PATCH 1/2] Document closure to fn coercion feature --- src/types.md | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) 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 From 4c475e0b1397f6d016730e0ac0a78d67d86e9148 Mon Sep 17 00:00:00 2001 From: est31 Date: Sat, 27 May 2017 16:36:58 +0200 Subject: [PATCH 2/2] Document it also in type-coercions.md --- src/type-coercions.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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