[rust-dev] std::num::pow() is inadequate / language concepts

Patrick Walton pcwalton at mozilla.com
Fri Jul 25 10:34:03 PDT 2014


On 7/25/14 10:10 AM, Josh Haberman wrote:
> On Fri, Jul 25, 2014 at 10:04 AM, Patrick Walton <pcwalton at mozilla.com> wrote:
>> Neither auto-ref or ad-hoc operator overloading
>> would let you write a generic function that calls
>> `pow` and works optimally with both bigints and
>> ints. I think the only thing that would work is
>> something like C++ ad-hoc templates, which is
>> a road I don't want to go down.
>
> Could you explain what you mean by "ad-hoc templates", and how this
> differs from Rust's templates?

In Rust you can never have type errors during template expansion. If a 
call to a generic/template typechecks properly, then the template is 
guaranteed to expand to valid Rust code with no type errors within it. 
This is done via the trait system, which is similar in spirit to the 
concept systems proposed for C++17 (the difference being that Rust 
*only* has concepts).

The primary benefit of this setup is that the infamous template error 
messages in C++ are eliminated. There are a bunch of other secondary 
benefits as well: there is no need for the ADL hack, you can do things 
like overload on the return type, etc.

Patrick



More information about the Rust-dev mailing list