Rust 中的函数原型是什么?
What is a function prototype in Rust?
我想了解 Rust 中 #[inline]
属性的行为,所以我通读了 the Attributes section of The Rust Reference。这很有帮助,但我发现这部分描述令人困惑(强调我的):
The inline
attribute suggests to the compiler that it should place a copy of the attributed function in the caller, rather than generating code to call the function where it is defined.
This attribute can be used on functions and function prototypes, although it does not do anything on function prototypes.
针对 #[cold]
属性重复此警告。
我从未听说过关于 Rust 的术语“函数原型”。我知道 JavaScript 中有这样的概念,但是 JavaScript 的和 Rust 的对象和类型系统是非常不同的!这里是什么意思?
进一步搜索,我在 the Error Index 中发现了两次函数原型的提及:
E0034
The compiler doesn't know what method to call because more than one method has the same prototype.
E0580
The main
function was incorrectly declared. The main
function prototype should never take arguments.
在这种情况下,“函数原型”的意思似乎类似于“函数签名”——构成函数外部接口的名称、参数和类型。这似乎也是 what it means in the context of C/C++。但是,这似乎与上面的用法不符;每个函数定义都以函数的签名开头,所以说将属性放在签名上没有任何意义是没有意义的,因为这就是你在将属性放在函数上时所做的事情。
术语“函数原型”在 Rust 上下文中是什么意思?
However, that doesn't seem to match the usage above; every function definition starts with the function's signature, so it wouldn't make sense to say that putting the attribute on the signature does nothing, because that's what you're doing when you're putting the attribute on a function.
是的,每个函数都以签名开头,但并非每个签名都是函数定义的一部分。也就是说,可以有一个签名,但没有正文(例如在特征中),这就是您引用的文档中 "prototype" 的含义。像这样:
trait Foo {
#[inline] // This annotation does nothing
fn foo();
}
我想了解 Rust 中 #[inline]
属性的行为,所以我通读了 the Attributes section of The Rust Reference。这很有帮助,但我发现这部分描述令人困惑(强调我的):
The
inline
attribute suggests to the compiler that it should place a copy of the attributed function in the caller, rather than generating code to call the function where it is defined.This attribute can be used on functions and function prototypes, although it does not do anything on function prototypes.
针对 #[cold]
属性重复此警告。
我从未听说过关于 Rust 的术语“函数原型”。我知道 JavaScript 中有这样的概念,但是 JavaScript 的和 Rust 的对象和类型系统是非常不同的!这里是什么意思?
进一步搜索,我在 the Error Index 中发现了两次函数原型的提及:
E0034
The compiler doesn't know what method to call because more than one method has the same prototype.
E0580
The
main
function was incorrectly declared. Themain
function prototype should never take arguments.
在这种情况下,“函数原型”的意思似乎类似于“函数签名”——构成函数外部接口的名称、参数和类型。这似乎也是 what it means in the context of C/C++。但是,这似乎与上面的用法不符;每个函数定义都以函数的签名开头,所以说将属性放在签名上没有任何意义是没有意义的,因为这就是你在将属性放在函数上时所做的事情。
术语“函数原型”在 Rust 上下文中是什么意思?
However, that doesn't seem to match the usage above; every function definition starts with the function's signature, so it wouldn't make sense to say that putting the attribute on the signature does nothing, because that's what you're doing when you're putting the attribute on a function.
是的,每个函数都以签名开头,但并非每个签名都是函数定义的一部分。也就是说,可以有一个签名,但没有正文(例如在特征中),这就是您引用的文档中 "prototype" 的含义。像这样:
trait Foo {
#[inline] // This annotation does nothing
fn foo();
}