3 ms·
In modern C++, you can write: template<typename T, typename U> auto myFn(T x, U y){ return x + y; } Note the return type of the function is
by logicchains 6y ago
In modern C++, you can write:
template<typename T, typename U>
auto myFn(T x, U y){
return x + y;
}
Note the return type of the function is not specified; it could return anything, depending on what the type of x + y is. In Rust you can't do that; you must specify the return type of the function.
- jasode 6y agoThat's not a good C++ example for comparison because that's using templates. The whole point of templates is to have "code that generates other code". Therefore, omitting the types as much as possible lets the compiler deduce types so the programmer doesn't have to manually write multiple redundant versions with different types. With normal functions, one parameterizes values but templates lets you parameterize types so explicitly putting a return type defeats the purpose of templates. In the context of the Rust blog, the comment about type signatures seems to be about ABI Abstract Binary Interface backward compatibility of libraries and not templates generating code.
- logicchains 6y agoIt also works without templates: auto add(int a, int b){ return a + b; } But yes there's usually less reason to do it for a normal function.