Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

函数指针类型

一个函数指针类型,使用 fn 关键字编写,指向一个在编译时身份不一定确定的函数。

一个将 Binop 定义为函数指针类型的示例:

#![allow(unused)]
fn main() {
fn add(x: i32, y: i32) -> i32 {
    x + y
}

let mut x = add(5,7);

type Binop = fn(i32, i32) -> i32;
let bo: Binop = add;
x = bo(5,7);
}

函数指针可以通过来自 函数项 以及非捕获、非异步的 闭包隐式类型转换 来创建。

unsafe 限定符表示该类型的值是一个 不安全函数 ,而 extern 限定符表示它是一个 外部函数

要使函数成为变长参数函数,其 extern ABI 必须是 items.extern.variadic.conventions 中列出的之一。

函数指针参数上的属性

函数指针参数上的属性遵循与 常规函数参数 相同的规则和限制。