15-函数指针类型
函数指针类型 — The Rust Reference
1分钟内可阅读完
译文 · 基于 The Rust Reference
原文链接: https://doc.rust-lang.org/reference/types/function-pointer.html
r[type.fn-pointer]
函数指针类型
r[type.fn-pointer.syntax]
BareFunctionType ->
ForLifetimes? FunctionTypeQualifiers `fn`
`(` FunctionParametersMaybeNamedVariadic? `)` BareFunctionReturnType?
FunctionTypeQualifiers -> `unsafe`? (`extern` Abi?)?
BareFunctionReturnType -> `->` TypeNoBounds
FunctionParametersMaybeNamedVariadic ->
MaybeNamedFunctionParameters | MaybeNamedFunctionParametersVariadic
MaybeNamedFunctionParameters ->
MaybeNamedParam ( `,` MaybeNamedParam )* `,`?
MaybeNamedParam ->
OuterAttribute* ( ( IDENTIFIER | `_` ) `:` )? Type
MaybeNamedFunctionParametersVariadic ->
( MaybeNamedParam `,` )* MaybeNamedParam `,` OuterAttribute* `...`
r[type.fn-pointer.intro]
函数指针类型使用 fn 关键字书写,指的是其身份在编译时不一定已知的函数。
下面的例子中,Binop 被定义为函数指针类型:
| |
r[type.fn-pointer.coercion] 函数指针可以通过强制转换从函数项以及不捕获、非 async 的闭包创建。
r[type.fn-pointer.qualifiers]
unsafe 限定符表示该类型的值是一个 unsafe 函数,而 extern 限定符表示它是一个 extern 函数。
r[type.fn-pointer.constraint-variadic]
要使函数为可变参数函数,其 extern ABI 必须是 [items.extern.variadic.conventions] 中列出的 ABI 之一。
r[type.fn-pointer.extern-custom]
extern "custom" 函数指针必须遵循 [items.fn.extern.custom.signature] 中的规则。
r[type.fn-pointer.attributes]
函数指针参数上的属性
函数指针参数上的属性遵循与普通函数参数相同的规则和限制。