Fundamentals 7 min read

Can Rust Outrun C? Exploring When Rust Beats C in Performance

This article examines a Reddit question about whether Rust can be faster than C under identical conditions, discussing inline assembly, struct layout differences, safety checks, compile‑time versus run‑time behavior, and concluding that there is no inherent speed advantage.

21CTO
21CTO
21CTO
Can Rust Outrun C? Exploring When Rust Beats C in Performance

Recently a Reddit user asked: "Under otherwise equal conditions, what method can make a Rust implementation faster than a C implementation?" The author explores this complex question, noting that the answer depends on the precise meaning of "everything being equal".

In other words, comparing languages becomes difficult because "same" and "different" can be argued in many ways that affect runtime performance.

Inline Assembly

Rust has built‑in inline assembly, while C treats it as a common compiler extension.

Example of a Rust function (using inline assembly) that reads the timestamp counter:

#include <stdint.h>

uint64_t rdtsc(void) {
    uint32_t lo, hi;
    __asm__ __volatile__(
        "rdtsc"
        : "=a"(lo), "=d"(hi)
    );
    return ((uint64_t)hi << 32) | lo;
}

The same functionality compiled with rustc 1.87.0 and clang 20.1.0 yields the following assembly (viewable on Godbolt):

rdtsc:
    rdtsc
    shl     rdx, 32
    mov     eax, eax, rdx
    or      rax, rdx
    ret

Similar Code, Different Results

Rust and C can produce different semantics for similar code. Consider the following Rust struct:

struct Rust {
    x: u32,
    y: u64,
    z: u32,
}

And the equivalent C struct:

struct C {
    uint32_t x;
    uint64_t y;
    uint32_t z;
};

On x86_64, the Rust struct occupies 16 bytes because Rust may reorder fields to minimize size, whereas the C struct occupies 24 bytes.

Social Factors

Some developers report that Rust's safety checks encourage them to write code that would be considered "dangerous" in C, because in C they would need to add extra copies for safety. The Mozilla Stylo project is cited: attempts to parallelize Firefox's style layout in C++ failed twice, but succeeded when rewritten in Rust.

The discussion also raises the question of whether a developer's expertise influences performance: a Rust expert versus a C expert may produce faster code in their preferred language, but this is not a language‑intrinsic advantage.

Compile‑time vs Run‑time

A follow‑up Reddit comment asks whether Rust's safety checks are all compile‑time and thus have no runtime impact. The answer is nuanced: many checks are compile‑time, but some (e.g., bounds checks) occur at runtime unless the compiler can prove they are unnecessary and optimizes them away. In C, similar checks must be written manually and can also be optimized away if proven safe.

Using unchecked indexing in Rust ( array.get_unchecked(0)) yields C‑like semantics, while achieving Rust‑like safety in C requires explicit checks.

Conclusion

If we assume C is the "fastest" language, does Rust ever match it for the same task?

Are there internal reasons why Rust cannot do the same thing?

The author concludes that, ignoring inline assembly, there is essentially no inherent speed difference between Rust and C; the answer is "no"—they are equally fast. However, real‑world performance depends on many variables such as project constraints, developer skill, and engineering context, making universal statements difficult.

Author: Steve Klabnik Translation: Luo Yi Original article: https://steveklabnik.com/writing/is-rust-faster-than-c
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceRustClanguage comparisoninline assembly
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.