Is Rust faster than C?

https://news.ycombinator.com/rss Hits: 11
Summary

Is Rust faster than C? Jun 9, 2025 Someone on Reddit recently asked: What would make a Rust implementation of something faster than a C implementation, all things being the same? I think this is a great and interesting question! It’s really tough because it ultimately relies on what exactly you mean by “all things being the same.” And I think this is something that makes it hard to compare languages. Here’s some ways in which you can argue that things are “the same” but also, that they’re not, and what they imply for runtime performance. Inline Assembly Rust has inline assembly built into the language. C has inline assembly as a very common compiler extension, to the degree where saying it’s not part of the language is an arguable nitpick. Here’s an example in Rust: use std::arch::asm; #[unsafe(no_mangle)] pub fn rdtsc() -> u64 { let lo: u32; let hi: u32; unsafe { asm!( "rdtsc", out("eax") lo, out("edx") hi, options(nomem, nostack, preserves_flags), ); } ((hi as u64) << 32) | (lo as u64) } This reads the time stamp counter with rdtsc, and returns its value. Here’s the example in C: #include <stdint.h> uint64_t rdtsc(void) { uint32_t lo, hi; __asm__ __volatile__ ( "rdtsc" : "=a"(lo), "=d"(hi) ); return ((uint64_t)hi << 32) | lo; } These (under -0 in both rustc 1.87.0 and clang 20.1.0) produce the same assembly: rdtsc: rdtsc shl rdx, 32 mov eax, eax or rax, rdx ret Here’s a link on Godbolt: https://godbolt.org/z/f7K8cfnx7 Does this count? I don’t know. I don’t think it really speaks to the question asked, but it is one way to answer the question. Similar code, different results Rust and C can have different semantics for similar code. Here’s a struct in Rust: struct Rust { x: u32, y: u64, z: u32, } Here’s “the same” struct in C: struct C { uint32_t x; uint64_t y; uint32_t z; }; In Rust, this struct is 16 bytes (on x86_64, again) and in C, it is 24. This is because Rust is free to reorder the fields to optimize for size, while C is not. Is this the same, or different? ...

First seen: 2026-01-14 13:09

Last seen: 2026-01-15 06:15