@@ -230,11 +230,60 @@ varieties of pointer in Rust:
230
230
The standard library contains additional 'smart pointer' types beyond references
231
231
and raw pointers.
232
232
233
- ## Function types
233
+ ## Function item types
234
234
235
- The function type constructor ` fn ` forms new function types. A function type
236
- consists of a possibly-empty set of function-type modifiers (such as ` unsafe `
237
- or ` extern ` ), a sequence of input types and an output type.
235
+ When referred to, a function item yields a zero-sized value of its
236
+ _ function item type_ . That type explicitly identifies the function - its name,
237
+ its type arguments, and its early-bound lifetime arguments (but not its
238
+ late-bound lifetime arguments, which are only assigned when the function
239
+ is called) - so the value does not need to contain an actual function pointer,
240
+ and no indirection is needed when the function is called.
241
+
242
+ There is currently no syntax that directly refers to a function item type, but
243
+ the compiler will display the type as something like ` fn() {foo::<u32>} ` in error
244
+ messages.
245
+
246
+ Because the function item type explicitly identifies the function, the item
247
+ types of different functions - different items, or the same item with different
248
+ generics - are distinct, and mixing them will create a type error:
249
+
250
+ ``` rust,ignore
251
+ fn foo<T>() { }
252
+ let x = &mut foo::<i32>;
253
+ *x = foo::<u32>; //~ ERROR mismatched types
254
+ ```
255
+
256
+ However, there is a [ coercion] from function items to [ function pointers] ( #function-pointer-types )
257
+ with the same signature, which is triggered not only when a function item
258
+ is used when a function pointer is directly expected, but also when different
259
+ function item types with the same signature meet in different arms of the same
260
+ ` if ` or ` match ` :
261
+
262
+ [ coercion ] : type-coercions.html
263
+
264
+ ``` rust
265
+ # let want_i32 = false ;
266
+ // `foo_ptr_1` has function pointer type `fn()` here
267
+ let foo_ptr_1 : fn () = foo :: <i32 >;
268
+
269
+ // ... and so does `foo_ptr_2` - this type-checks.
270
+ let foo_ptr_2 = if want_i32 {
271
+ foo :: <i32 >
272
+ } else {
273
+ foo :: <u32 >
274
+ };
275
+ ```
276
+
277
+ ## Function pointer types
278
+
279
+ Function pointer types, created using the ` fn ` type constructor, refer
280
+ to a function whose identity is not necessarily known at compile-time. They
281
+ can be created via a coercion from both [ function items] ( #function-item-types )
282
+ and non-capturing [ closures] ( #closure-types ) .
283
+
284
+ A function pointer type consists of a possibly-empty set of function-type
285
+ modifiers (such as ` unsafe ` or ` extern ` ), a sequence of input types and an
286
+ output type.
238
287
239
288
An example of a ` fn ` type:
240
289
@@ -250,22 +299,6 @@ let bo: Binop = add;
250
299
x = bo (5 ,7 );
251
300
```
252
301
253
- ### Function types for specific items
254
-
255
- Internal to the compiler, there are also function types that are specific to a particular
256
- function item. In the following snippet, for example, the internal types of the functions
257
- ` foo ` and ` bar ` are different, despite the fact that they have the same signature:
258
-
259
- ``` rust
260
- fn foo () { }
261
- fn bar () { }
262
- ```
263
-
264
- The types of ` foo ` and ` bar ` can both be implicitly coerced to the fn
265
- pointer type ` fn() ` . There is currently no syntax for unique fn types,
266
- though the compiler will emit a type like ` fn() {foo} ` in error
267
- messages to indicate "the unique fn type for the function ` foo ` ".
268
-
269
302
## Closure types
270
303
271
304
A [ closure expression] ( expressions.html#closure-expressions ) produces a closure
0 commit comments