简体   繁体   中英

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 . 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.

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.

I've never heard the term "function prototype" used with respect to Rust. I know that such a concept exists in JavaScript, but JavaScript's and Rust's object and type systems are very different! What does it mean here?

Searching further, I found two mentions of function prototypes in 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.

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++ . 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?

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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