G.FMT.01 extern
外部函数应显式指定 ABI 标识
【级别】 要求
【描述】
当使用 extern
指定外部函数时,应显式指定 ABI
标识,否则默认为 C-ABI
。 显式指定 ABI 标识是 Rust 语言的一种约定俗成,有利于代码健壮性。
【反例】
Rust
use std::os::raw::c_char;
// 不符合:不要省略 C-ABI 指定
extern {
fn strlen(s: *const c_char) -> i32;
}
【正例】
Rust
use std::os::raw::c_char;
// 符合
extern "C" {
fn strlen(s: *const c_char) -> i32; }
// 符合
extern "Rust" {
type MyType;
fn f(&self) -> usize;
}