Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 653
Jeremy Avatar asked Jan 10 '19 16:01

Jeremy


People also ask

What is the function of a prototype?

1) It tells the return type of the data that the function will return. 2) It tells the number of arguments passed to the function. 3) It tells the data types of each of the passed arguments. 4) Also it tells the order in which the arguments are passed to the function.

What is a function prototype and why do you need it?

The function prototypes are used to tell the compiler about the number of arguments and about the required datatypes of a function parameter, it also tells about the return type of the function. By this information, the compiler cross-checks the function signatures before calling it.

What is a function in Rust?

Functions are the building blocks of readable, maintainable, and reusable code. A function is a set of statements to perform a specific task. Functions organize the program into logical blocks of code.

What is the difference between a function and a function prototype?

While a function definition specifies how the function does what it does (the "implementation"), a function prototype merely specifies its interface, i.e. what data types go in and come out of it.


1 Answers

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();
}
like image 174
sepp2k Avatar answered Oct 08 '22 03:10

sepp2k