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 中列出的那些之一。

函数指针参数上的属性

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