r/AskProgramming • u/Objective-Farmer4183 • May 03 '26
Architecture When is C better than Rust?
I've seen a lot of people saying that Rust = C but more secure. Is that true? Is there any catch or something? If you've used both of these programming languages, what was your experience with each? Which ones did you prefer?
I am writing this because I still don't know in which language should I code my backend for a website, and I don't want to mess things up. I am choosing low level because I want performance and low RAM usage + I am more familiar with low level languages.
TLDR; When do you think I should rather use C over Rust and vice versa?
7
u/huuaaang May 03 '26
I would never choose C for a website backend. That sound tedious. I probably wouldn't use Rust either. But Rust before C for this task.
My go to for compiled web backend/service would be Go. It's basically built for this.
I'd only really use C for Linux kernel stuff or esp32/Arduino programming. Few things really need to be that small and optimized these days.
I'd use C++ for game programming.
56
u/Pseudanonymius May 03 '26
When the code already exists.
42
u/Own_Age_1654 May 03 '26
When the rest of the project is in C.
4
u/AntimatterTNT May 03 '26
either never or always, it's being debated among the elder council.
if you're already familiar with both you should have the answer already. c has way more libraries and is the de facto interoperability standard for cross language calls. rust is modern and has more comforts, whether or not it's memory model is part of those comforts is almost irrelevant to what the language offers over c (or c++), at least to me.
if you want to use this as a learning experience then the clear answer is c. there are almost no openings for rust developers and even if the tides will shift eventually we're probably decades away from rust being as prevelant as c (if it ever will be, which i doubt)
13
u/helpprogram2 May 04 '26
When you’re an expert at c and not at rust
3
u/BotBuilderVenture May 03 '26
C is technically "better" only when you're working on ancient hardware with no Rust compiler support, writing extremely tiny embedded drivers where every byte of binary size is a war, or if you need to interface with a massive legacy codebase where the FFI overhead isn't worth the hassle.
17
u/FruitdealerF May 03 '26
C is better if you need to compile for a platform that rust doesn't support. It also has some advantages if you're building a VM (byte code) and you're looking to squeeze the absolute maximum amount of performance. In other words if you need to use tons and tons of unsafe code rust becomes more of a pain.
For anything web related definitely use Rust > C, the ecosystem is multiple orders of magnitude more developer friendly.
4
u/me_myself_ai May 03 '26
When you need a library that doesn’t exist in rust
4
u/Interesting_Buy_3969 May 04 '26
For a website backend - definitely Rust. Without a doubt.
3
u/mredding May 05 '26
In a word: yes. But in reality: no.
The problem is our language to talk about the subject. There is depth and nuance that is just lost in the summary. C can make perfectly secure software - if you code correctly. "Just stop writing shit software!" But Rust makes it easier to write... "more-correct" software.
Rust has that borrow-checker, which is a pretty neat feature, but that's only ONE type of safety. Security audits of Rust show comparable rates of exploits and incidents as other languages. It's better, and I would say if you had nothing else to go on, you'd probably want to start with Rust, but you still can't be a dumb-ass.
Don't be lured into a false sense of confidence, misled by some silver-bullet solution that over-promises.
All sorts of little catches! Rust is a much larger, more complicated language. Rust was born as a protest against C++ ~2008-2009, because the damn standards committee couldn't get what would eventually become C++11 out the door, and the changes that DID make it into C++11 weren't radical enough.
So the language is comparably complex more to C++ than C.
C is a dead simple language. It's small and has a weak type system. That's why it's perfectly amenable to being ported to literally every CPU architecture invented since ~1971. You invent a new architecture and you publish 1) the instruction set, 2) an assembler, 3) a C compiler, and 4) if you're lucky, and you have the time, a Fortran compiler - to show you're fucking serious.
The C language has been the language of operating systems since nearly its inception. Almost every system ABI is defined by their C support. If you're going to write system software for an OS or platform, it's going to speak like C to the rest of the world. In this way - it doesn't matter what your implementation language is.
The more your language does, the harder it is to do anything else/to the contrary. This is why you don't see operating systems in all manner of languages on all manner of hardware.
You WANT Undefined Behavior - as much as possible. The less the language does for you, the more assumptions it can make, the more aggressively it can optimize. Once you define the behavior of integer overflow - that's it, you're fucked. Now the compiler has to generate additional code to ensure that guarantee holds. You also want a language library that protects you from tripping into UB...
So Rust is quite performant - being a compiled systems language, but the community is trying to validate their existence by rewriting everything around them. Their stretch goal is to rewrite
ffmpegto be JUST AS FAST as the mature and proven robust 40 year old library...That's typically the sort of behavior we discourage.
Rust CAN be frustrating, as the borrow checker tends to get in the way, so you have to write in a particular way, which can be frustratingly verbose. Safe - sure, and it's hard to argue against that, but verbose. Makes you wonder about the alternatives - perhaps a language that doesn't assume I'm a god damn idiot? But don't let our egos get ahead of us - maybe I am a god damn idiot and don't know it. I feel like a lot of people write C++ out of spite for themselves, rather than try to make solid code. They're not as responsible as they ought to be.
I'm not opposed to Rust. Despite it's youth and arrogant community - born out of a tantrum, and doing the same god damn thing every other language has ever done - billed itself as a C or C++ killer, it has good ideas and will find its niche.
I'm getting older and I'm quite invested in C and C++. I don't need more languages trying to capture market share. I don't care. Regardless, I'll code in whatever language you people pay me to. It's not so much about preference for me, but experience.
Rust should be fine. C++ isn't a bad idea. Golang is a fun language and it transpiles to C.
You're going to mess it up. Just come to accept that now. You don't HAVE a backend, so you don't know right from wrong until you GET THERE and find out. You code for the problem you have - which is you have nothing at all, so ANYTHING is better than nothing, as it will give you perspective. You need SOMETHING to compare to, to know what you did right vs. what you did wrong.
No one goes from NOTHING to infinitely scalable globally distributed decentralized architecture. You don't need it, there's no demand for it, you REALLY don't want that kind of investment and cost of operations, and such big platforms don't scale down to just you and a few friends/occasional employers.
Performance is probably overstated. You don't even have anything to compare against. You don't know what's fast or slow because nothing is running. You're worrying too much about phantoms. Make something real first.
I have my reservations about Rust's
unsafefeature. If you're going to do that, you're basically just dropping down to C. So why not write in C? People have a REAL knee-jerk reaction against mixed language environments. Heaven forbid just a few functions are written in Lisp or Fortran...1
u/Far_Archer_4234 May 03 '26
The alphabet.
1
u/ibrown39 May 03 '26
Yes
0
u/MatJosher May 03 '26
For a website backend choose Rust over C.
1
u/pixel293 May 03 '26
First I prefer Rust, I feel like I have less bugs when I program in Rust.
That said, where Rust breaks down (or at least you need to use unsafe a lot) is when data has multiple owners. It's easiest to see this in data structures. Write a safe doubly linked list, it's is pretty annoying and there is overhead. Also if you don't implement a drop() method, you can exhaust the stack when it's dropped. Another one would be a B tree with parent pointers. You can write both these algorithms with unsafe blocks, but then you are basically throwing away some safety.
Another thing to consider are 3rd party libraries, C has a ton of libraries, battle tested libraries. Rust has libraries as well, but not as much as C, and they may or may not be battle tested.
1
u/FloydATC May 04 '26
Any non-trivial C code is almost guaranteed to have bugs, and any bug in C is almost guaranteed to be exploitable in some way or another at some point in the future. Thousands of people can all agree there's absolutely no way this particular piece of code could fail and then two decades later it blows up in a spectacular way.
Rust is almost guaranteed to make such bugs impossible, but this also means that many things that are easy in C require significant effort in Rust because you now you can't simply throw pointers around until things appear to work.
...Or you could use "unsafe" but then you might as well just use C because all bets are off.
I always say you're not ready to use Rust until you've spent a few weeks trying to figure out some impossible bug in C or C++; only then can you truly appreciate that the Rust compiler is actually just helping you prevent those bugs.
1
u/Overall-Screen-752 May 04 '26
For the backend of a website? Use golang. Phenomenal API frameworks and OoTB features, native concurrency, about the same speed as rust. Worth considering
1
u/_N-iX_ May 04 '26
They solve slightly different problems. C is great when you need absolute control, minimal dependencies, or are working in constrained/embedded environments. Rust is better when you care about preventing entire classes of bugs and maintaining a codebase over time. For a web backend, the safety + ecosystem in Rust usually outweigh what you gain from going lower-level with C.
1
u/TheRNGuy May 04 '26
Rust is more like C++, not C.
1
u/EfficientMongoose317 May 04 '26
tbh C is better when you need absolute control and minimal overhead, like embedded systems, kernels, or very tight performance constraints. It’s simpler, predictable, and the ecosystem is everywhere
Rust shines when you want that low level performance but with safety. Memory bugs, race conditions, that stuff gets caught early, which matters a lot in larger or long running systems. For a backend, though, imo neither is the usual choice unless you have a very specific reason. You’ll spend way more time dealing with low level stuff instead of building features
So rough rule:
C → maximum control, minimal abstraction
Rust → safety + performance balance
But for web backends, something higher level is usually more practical unless you really know why you’re going low level
1
u/AliceCode May 04 '26
If you don't know the answer to this question, you should probably choose Rust. You can use libraries in C, but C is less batteries included. Rust has a huge standard library full of high level constructs. Rust also has a package manager with a decently large ecosystem. Nothing beats C compatibility, but you won't be missing anything by choosing Rust because you can always write any piece of the software in C if you choose. Same goes for C, too. You can use Rust for parts of your C program that need more care.
They are both wonderful languages, and you can get a lot of mileage with both of them, but Rust will have more of a batteries included feel.
I am biased, though, as Rust is my language of choice for nearly all of my projects. I only have one project written in C, and that's just a personal "standard" library that I've been working on for a month or so. I have a lot more experience with C++.
But you also have to consider that Rust comes with training wheels. You can't just freely mutate data willy nilly like you can in C. You also need to design your code carefully around working with lifetimes, which will often lead to the flattening of data (hierarchical data structures can introduce subtle difficulties with the borrow checker).
Maybe write a project in both languages to completion. Same project, different languages. When you've finished the project in both languages, you can decide which you want to work with.
1
u/Albedo101 May 05 '26
When you use lots of 3rd party libraries, do gamedev and need to cooperate with the C++ folks. Or cooperate with anyone, really.
1
u/zsaleeba May 06 '26
When you're writing a self-hosting C compiler
1
u/TapEarlyTapOften May 06 '26
When it needs to be maintained 5 years from now by someone that didn't write it.
1
u/qrzychu69 May 06 '26
C (and others like it) are better in cases where the Rust safety is an inconvenience, here is a good example: https://gist.github.com/rtfeldman/77fb430ee57b42f5f2ca973a3992532f
They went with Zig, which is kind of "C but not as tedious"
1
u/TartOk3387 May 06 '26
Maybe controvertial: when the type-safety and memory-safety of your program are too complex to be deduced by the Rust type and borrow checker.
All type systems are conservative approximations: the necessarily reject some programs that are safe.
Rust has unsafe blocks for this reason. But if your whole program is a giant unsafe block, then you might as well write in C.
I doubt there are many situations where the complexity is such that you couldn't at least factor out some portion into safe Rust, but I'm sure some exist somewhere.
1
u/airafterstorm May 07 '26
Definitely for stable ABI like Win32, Linux syscall, Mac OS platform ABI
1
u/marrsd May 07 '26
When you care about efficient memory management. Rust is built around the concept of RAII. This isn't always desirable.
Casey Muratori gives a very good talk on this here: https://www.youtube.com/watch?v=xt1KNDmOYqA
1
u/UnknownEssence May 05 '26
Why would you use a low level language for a backend website? Just vibe code it with NodeJS. JavaScript ok the front and back end.
0
u/BenchEmbarrassed7316 May 03 '26
In the vast majority of cases, you can replace a program written in C with a program written in Rust and only get benefits.
None of these languages:
Arc<Mutex<T>>and.clone()with a whole lot of spaghetti)Imagine someone coming to a university and saying they're going to write a paper on a complex mathematical problem. And then they specify which numerals, Roman or Arabic, are better to use for this. And what's the difference between them.
-1
u/skeletal88 May 03 '26
Why write website backends in rust? Why not something simpler like python or java.
0
u/RRumpleTeazzer May 03 '26
essentially yes. Rust was created with C compatibility in mind.
Rust will essentially break when you cross FFI barriers to native C and back, as you need to reason what the C code would do. These assumptions can be wrong and then Rust runs amok.