简体   繁体   English

什么是Rust的函数原型?

[英]What is a function prototype in Rust?

I wanted to understand the behaviour of the #[inline] attribute in Rust, so I was reading through the Attributes section of The Rust Reference . 我想了解Rust中#[inline]属性的行为,所以我正在阅读The Rust Reference的Attributes部分 It was very helpful, but I found this part of the description confusing (emphasis mine): 这非常有帮助,但我发现这部分描述令人困惑(强调我的):

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. inline属性向编译器建议它应该在调用者中放置属性函数的副本,而不是生成代码来调用定义它的函数。

This attribute can be used on functions and function prototypes, although it does not do anything on function prototypes. 此属性可用于函数和函数原型,但它不对函数原型执行任何操作。

This caveat is repeated for the #[cold] attribute. #[cold]属性重复此警告。

I've never heard the term "function prototype" used with respect to Rust. 我从未听过关于Rust使用的术语“函数原型”。 I know that such a concept exists in JavaScript, but JavaScript's and Rust's object and type systems are very different! 我知道这样的概念存在于JavaScript中,但JavaScript和Rust的对象和类型系统是非常不同的! What does it mean here? 这是什么意思?

Searching further, I found two mentions of function prototypes in the Error Index : 进一步搜索,我在错误索引中找到了两个函数原型:

E0034 E0034

The compiler doesn't know what method to call because more than one method has the same prototype. 编译器不知道要调用哪种方法,因为多个方法具有相同的原型。

E0580 E0580

The main function was incorrectly declared. main功能被错误地声明。 The main function prototype should never take arguments. main函数原型永远不应该参数。

In this case, "function prototype" seems to mean something like "function signature" -- the names, arguments, and types that make up a function's external interface. 在这种情况下,“函数原型”似乎意味着类似“函数签名” - 构成函数外部接口的名称,参数和类型。 This also appears to be what it means in the context of C/C++ . 这似乎也是它在C / C ++环境中的含义 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. 每个函数定义都以函数的签名开头,因此将该属性放在签名上没有任何意义是没有意义的,因为当你将属性放在函数上时,这就是你正在做的事情。

What does the term "function prototype" mean in the context of Rust? 什么术语“函数原型”在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. 每个函数定义都以函数的签名开头,因此将该属性放在签名上没有任何意义是没有意义的,因为当你将属性放在函数上时,这就是你正在做的事情。

Yes, every function starts with a signature, but not every signature is part of a function definition. 是的,每个函数都以签名开头,但并非每个签名都是函数定义的一部分。 That is, it is possible to have a signature, but no body (in a trait for example) and that's what is meant by "prototype" in the documentation you cited. 也就是说,可以有一个签名,但没有正文(例如,在特征中),这就是你引用的文档中“原型”的含义。 Something like this: 像这样的东西:

trait Foo {
    #[inline] // This annotation does nothing
    fn foo();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM