3 ms·
Rust has several specific areas where it is faster by default than C and C++, thanks to knowledge of ownership in the type system. The most dramatic example of
by kibwen 12y ago
Rust has several specific areas where it is faster by default than C and C++, thanks to knowledge of ownership in the type system. The most dramatic example of this is that almost every pointer in Rust can be automatically and safely proven not to alias any other pointer, which is the equivalent of sticking `restrict` on every pointer in a C program. Sadly, Rust isn't actually passing this information onto LLVM just yet, so it's not generating code nearly as well as it could. :P
On the other hand, Rust also has specific areas where it is slower by default than C or C++. The most dramatic example here is that array bounds access is checked by default, with an unsafe method call for doing unchecked access. It strives to bypass this restriction by leaning heavily on iterators, which are an abstraction that allow you to safely omit the bounds check.
In the end, all that matters is that both languages offer zero-cost abstractions, so the ultimate arbiter of speed will be the optimizer on the backend. Saying that Rust is "faster than C++" is definitely premature at this point; I'll reserve that judgment for when it at last starts making use of all its free aliasing info.