]>
Commit | Line | Data |
---|---|---|
13cf67c4 XL |
1 | # Function pointer types |
2 | ||
3 | > **<sup>Syntax</sup>**\ | |
4 | > _BareFunctionType_ :\ | |
5869c6ff | 5 | > [_ForLifetimes_]<sup>?</sup> _FunctionTypeQualifiers_ `fn`\ |
13cf67c4 XL |
6 | > `(` _FunctionParametersMaybeNamedVariadic_<sup>?</sup> `)` _BareFunctionReturnType_<sup>?</sup> |
7 | > | |
5869c6ff XL |
8 | > _FunctionTypeQualifiers_:\ |
9 | > `unsafe`<sup>?</sup> (`extern` [_Abi_]<sup>?</sup>)<sup>?</sup> | |
10 | > | |
13cf67c4 XL |
11 | > _BareFunctionReturnType_:\ |
12 | > `->` [_TypeNoBounds_] | |
13 | > | |
14 | > _FunctionParametersMaybeNamedVariadic_ :\ | |
15 | > _MaybeNamedFunctionParameters_ | _MaybeNamedFunctionParametersVariadic_ | |
16 | > | |
17 | > _MaybeNamedFunctionParameters_ :\ | |
18 | > _MaybeNamedParam_ ( `,` _MaybeNamedParam_ )<sup>\*</sup> `,`<sup>?</sup> | |
19 | > | |
20 | > _MaybeNamedParam_ :\ | |
e74abb32 | 21 | > [_OuterAttribute_]<sup>\*</sup> ( ( [IDENTIFIER] | `_` ) `:` )<sup>?</sup> [_Type_] |
13cf67c4 XL |
22 | > |
23 | > _MaybeNamedFunctionParametersVariadic_ :\ | |
e74abb32 | 24 | > ( _MaybeNamedParam_ `,` )<sup>\*</sup> _MaybeNamedParam_ `,` [_OuterAttribute_]<sup>\*</sup> `...` |
13cf67c4 XL |
25 | |
26 | Function pointer types, written using the `fn` keyword, refer to a function | |
27 | whose identity is not necessarily known at compile-time. They can be created | |
28 | via a coercion from both [function items] and non-capturing [closures]. | |
29 | ||
30 | The `unsafe` qualifier indicates that the type's value is an [unsafe | |
31 | function], and the `extern` qualifier indicates it is an [extern function]. | |
32 | ||
33 | Variadic parameters can only be specified with [`extern`] function types with | |
34 | the `"C"` or `"cdecl"` calling convention. | |
35 | ||
36 | An example where `Binop` is defined as a function pointer type: | |
37 | ||
38 | ```rust | |
39 | fn add(x: i32, y: i32) -> i32 { | |
40 | x + y | |
41 | } | |
42 | ||
43 | let mut x = add(5,7); | |
44 | ||
45 | type Binop = fn(i32, i32) -> i32; | |
46 | let bo: Binop = add; | |
47 | x = bo(5,7); | |
48 | ``` | |
49 | ||
e74abb32 XL |
50 | ## Attributes on function pointer parameters |
51 | ||
52 | Attributes on function pointer parameters follow the same rules and | |
53 | restrictions as [regular function parameters]. | |
54 | ||
416331ca | 55 | [IDENTIFIER]: ../identifiers.md |
5869c6ff | 56 | [_Abi_]: ../items/functions.md |
17df50a5 | 57 | [_ForLifetimes_]: ../trait-bounds.md#higher-ranked-trait-bounds |
416331ca XL |
58 | [_TypeNoBounds_]: ../types.md#type-expressions |
59 | [_Type_]: ../types.md#type-expressions | |
e74abb32 | 60 | [_OuterAttribute_]: ../attributes.md |
416331ca XL |
61 | [`extern`]: ../items/external-blocks.md |
62 | [closures]: closure.md | |
e1599b0c | 63 | [extern function]: ../items/functions.md#extern-function-qualifier |
416331ca | 64 | [function items]: function-item.md |
2b03887a | 65 | [unsafe function]: ../unsafe-keyword.md |
29967ef6 | 66 | [regular function parameters]: ../items/functions.md#attributes-on-function-parameters |