3 questions for c programmers using rust

  1. why rust?
    not meant to be an antagonistic question. i’ve personally been messing around with both rust and zig on side projects i’d otherwise use c on

  2. what has rust actually changed about the way you write systems code?
    generally, is there anything you approach fundamentally different in rust v c?

  3. anywhere you still prefer c to rust?
    basically, any particular kinds of projects, environments, api, or constraints where c is still the better choice, in your opinion?

Here's a C++ perspective (not strictly C):

  1. It really does comes down to Rust's unique selling point: guaranteed memory safety (with the 'unsafe' caveat) without sacrificing performance. This guarantee applies not only when you write the code, but as you (and others) are modifying it, which IMO is when a disproportionate number of bugs are introduced. It's hard to overstate how powerful this guarantee is.

Rust also has many nice extras (strong enums, pattern matching, Cargo, etc.), and they're great. But they're not game changers in the way that the "Rust guarantee" is.

  1. I rarely use C++ these days, but when I do, I make much more use of std::optional and std::tuple, now that Rust has shown me how useful these concepts are. I also tend to steer away from classes, in favour of pure functions.

  2. Can't say from personal experience, but I'd guess if you're writing a small, straightforward, single-threaded piece of code (maybe a very simple Linux kernel module), C might still be a better choice than Rust.

There are many reasons, but there was a specific point where I snapped and realized I needed to look at modern alternatives to C: I once realized that I had spent an entire day fiddling around with the build system for a project. I had a lot of actual coding to do, but I couldn't get on with it because I was trying to get the build system working on all our supported platforms. At that point, I realized the build system - that is supposed to make my life easier - was making my life more difficult and wasting time that I needed to put into writing actual code.

This was before I started using Rust, before I actually knew what cargo was. I had just read that modern languages made building easier, but I had no idea how much easier it would be.

Basically, Rust allows me to focus on actual programming instead of bureaucracy surrounding programming. It made programming much more fun for me. Also, I think I can write fairly robust code in C, but it takes a very long time. I can write even more robust code in Rust in a fraction of the time.

I reason a lot more around ownership and lifetimes in C code.

I tend to think further ahead than I used to. I used to just write code and reshape it as needed. After using Rust for a while, I find myself planning things out more.

Sure, there are a few cases. I have a package manager for an embedded platform that I actually have three versions of: A C version, a C++ version and a Rust version. When I build the package manager on the actual native platform, the C version builds in like 5 seconds. The C++ and Rust versions take roughly 800 quadrillion years to build, despite them not actually having more features. (Though the runtime of the tools are basically the same, it's just building them that are a nightmare).

Also, kernel drivers. (But that won't be true for much longer).

  1. I've been fed up with C's warts from 1970's that will never be fixed due to back compat. Windows vs Unix schism makes supporting both annoying (and means that even the few C warts that have been fixed take 10+ years to be usable in MSVC). Even simple dependencies are an endless struggle if you support more than a couple of Linux distros. It's so nice to leave fragmented build systems and baggage of the C preprocessor behind, and have have painless dependencies and build even large projects with the same command on all major platforms.

  2. I can make everything multithreaded by default without regretting it. I don't need Valgrind! The last time I had a memory leak was from integrating a library via a C API. My programs crash less than Python, but still run at C speed. I can just use a HashMap and unit tests without copypasting some single-header DIY macro hack thing. I can make network connections without a single #ifdef.

  3. For 16-bit CPUs maybe I'd use zig. I can't imagine going back to C.

Out of curiosity: what makes Zig such a good fit for 16-bit machines, and in particular, a better fit than Rust would be?
Is it Zig's cross-compilation support?

No, it's that 16-bit platforms are so tiny that you basically have your entire program in your head. Rust's features for large scale programs never apply. There's no OS, no allocator, no threads. Ecosystem and dependencies don't matter when you can't afford extra function calls or local variables.

So when every byte matters and you need to do unsafe hacks, you may as well do it in a less safe lang, and zig has better tooling than C.

Rust has only a specific niche where it's a king. For example, the guy picked Rust to write OS and ended with over 800 unsafe blocks.


So his Rust code has no benefits over C. So I would say - as long as you are not going to write OS, or OS drivers, you can choose Rust over C. Another criteria can be - zero unsafe blocks, do not read it literally.