File tree 2 files changed +20
-3
lines changed
2 files changed +20
-3
lines changed Original file line number Diff line number Diff line change 1
1
# Type coercions
2
2
3
- Coercions are defined in [ RFC 401] . A coercion is implicit and has no syntax.
3
+ Coercions are defined in [ RFC 401] . [ RFC 1558] then expanded on that.
4
+ A coercion is implicit and has no syntax.
4
5
5
6
[ RFC 401 ] : https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
7
+ [ RFC 1558 ] : https://github.com/rust-lang/rfcs/blob/master/text/1558-closure-to-fn-coercion.md
6
8
7
9
## Coercion sites
8
10
@@ -143,3 +145,5 @@ Coercion is allowed between the following types:
143
145
In the future, coerce_inner will be recursively extended to tuples and
144
146
structs. In addition, coercions from sub-traits to super-traits will be
145
147
added. See [ RFC 401] for more details.
148
+
149
+ * Non capturing closures to ` fn ` pointers
Original file line number Diff line number Diff line change @@ -286,9 +286,22 @@ more of the closure traits:
286
286
* ` Fn `
287
287
: The closure can be called multiple times through a shared reference.
288
288
A closure called as ` Fn ` can neither move out from nor mutate values
289
- from its environment. ` Fn ` inherits from ` FnMut ` , which itself
290
- inherits from ` FnOnce ` .
289
+ from its environment, but read-only access to such values is allowed.
290
+ ` Fn ` inherits from ` FnMut ` , which itself inherits from ` FnOnce ` .
291
291
292
+ Closures that don't use anything from their environment ("non capturing closures")
293
+ can be coerced to function pointers (` fn ` ) with the matching signature.
294
+ To adopt the example from the section above:
295
+
296
+ ``` rust
297
+ let add = | x , y | x + y ;
298
+
299
+ let mut x = add (5 ,7 );
300
+
301
+ type Binop = fn (i32 , i32 ) -> i32 ;
302
+ let bo : Binop = add ;
303
+ x = bo (5 ,7 );
304
+ ```
292
305
293
306
## Trait objects
294
307
You can’t perform that action at this time.
0 commit comments