In many benchmarks, Rust and C land close together. Both compile to optimized native code and carry little runtime overhead, so for many programs their speeds are similar. But “as fast as” depends on the algorithm, the compiler settings and the workload. Neither language is automatically faster, and a result on one task can reverse on another.
Most readers who ask this question mean the C language. Many of the same points apply to C++, because it also compiles to native code. If you are still sorting out how the two differ, read “Are C and C++ the same?” first. The short answer is no, although they share much of their syntax and tooling.
What “as fast as C” usually means
When people ask this, they usually mean something specific: given the same algorithm, the same input and optimizations turned on, how do the two programs compare? That is different from asking whether a particular library is fast, or whether a specific project is well written. Benchmarks often test sorting, parsing, hashing or number crunching, and the results can shift from one task to the next.
Why Rust can match C
Several features help Rust reach C-like speed:
- No garbage collector. Memory is freed at known points, so there are no unpredictable pauses to clean up after.
- Shared compiler backend. Rust uses LLVM by default, and C is often compiled with Clang, which also uses LLVM. Similar backends often produce similar machine code for similar patterns.
- Ownership information. A
&mutreference tells the compiler that no other reference is accessing the same data at that time. That promise lets the optimizer do more. C’srestrictkeyword offers a similar hint, but it is optional and easy to get wrong. - Zero-cost abstractions. Iterators and generic functions often compile down to the same loops you would write by hand.
Where Rust can cost you time
Rust’s safety checks are small but real:
- Bounds checks. Indexing with
values[2]checks the index and panics if it is out of range. - Overflow checks. Debug builds check for integer overflow. Release builds wrap around by default.
- Clones. Copying data to satisfy the borrow checker can cost far more than any check.
Here is the same small function in C and in Rust:
int get_third(const int *values) {
return values[2]; /* the caller must guarantee at least three values */
}
fn get_third(values: &[i32]) -> i32 {
values[2] // panics at runtime if there are fewer than three values
}
The Rust version adds a comparison at runtime, and that comparison can matter inside a very tight loop. Optimizers often remove checks they can prove are unnecessary, though. The C version is not safer for it. If the caller passes a short array, the function reads memory it does not own, which is undefined behavior. If you need a version that never panics, use values.get(2), which returns an Option<&i32>.
Where C can have an edge
C code is simple to reason about at the machine level, and experienced programmers can tune it closely without the language adding any checks. In a hot loop where the programmer has proven every index is valid, C can match or beat a straightforward Rust version. Rust can reach the same place with unsafe code, but then the safety rules become the programmer’s responsibility rather than the compiler’s.
How to test it yourself
- Write the same program in both languages, with the same algorithm and data sizes.
- Build with optimizations on:
gcc -O2orclang -O2for C, andcargo build --releasefor Rust. - Run each version several times and compare the spread of results, not just one number.
- Use a profiler, such as
perfon Linux, to see where the time goes. - Check that both programs produce the same output before you compare timings.
Make sure both programs are free of memory errors before you measure, because a broken program can give misleading timings. Tools such as Valgrind help here, and the question “Does Valgrind work for C++?” comes up often. For most C++ programs built for Linux, the answer is yes, although the program will run slower under the tool.
What to take away
For work where both languages compile to optimized native code, Rust is often about as fast as C. The language rarely decides speed by itself. Algorithms, data layout and the optimizer usually matter more, and a fair test on your own workload beats any published chart. If you are learning C or C++, the same skills help here too, such as understanding memory and how the compiler sees your code.