Does Rust improve your C and C++ code?

I'm very new to Rust. I've only written one program, which was a reimplementation of something I'd written in C# that was itself a copy of a book example. My recent experience, i.e., 10+ years, has been in the Microsoft .NET stack, though I did do C++ for several years before that.

But for those in the systems programming space have you found that, say, reimplementing a C or C++ component in Rust has helped uncover bugs or improved your technique when you return to C or C++?

I'm just curious.

Personally I have not found that getting into Rust has uncovered any bugs in the C/C++ I had written previously. However it did emphasise how it happened that past C/C++ projects I have worked on involving multiple developers over a number years did result in "interesting" bugs that caused problems after deployment and took significant time and effort to fix.

It also demonstrated that we don't have to live like that any more. There is a better way.

I can point to one example where this has happened. Cliff Biffle wrote a VGA graphics demo for tiny micro-controllers in C and then wrote the same again in Rust. During the course of which he realised at least one bug in his original C code. It's an interesting story: Rewriting m4vgalib in Rust - Cliffle

I cannot speak about returning to C or C++. I have no intension of ever doing so.



I didn't really return to C or C++, either. (I had to for a brief half year when I was teaching an introductory programming course in C++ last semester, but that's nothing serious, as you can probably imagine.)

However, since I found Rust, there are smaller or larger bits of language design that I am missing in every other language I use. For example, I miss macro-based metaprogramming in Swift. I miss value semantics in Python. I miss algebraic Optional and Result in C#. I miss coherence in Haskell, and so forth.

And this experience sometimes caused me to reimplement, or at least imitate, some of the features of Rust-the-language or Rust-the-wider-ecosystem that I love. For example, in a recent project, I coded up a type-safe serialization and deserialization system and a corresponding typed web server (HTTP handler) mechanism – and I did that in Python! (Yes, I was really desperate for types.)


I wouldn't say it's helped me uncover bugs, but after learning Rust I've noticed my C and C++ code is more reliable and has a simpler ownership story. I'm also a lot less likely to have shared mutability, which leads to code that is more functional and has less side-effects.

After things clicked in Rust I found it hard/uncomfortable to write C# using a "traditional" OO style because it relies on mutation and shared references, and everything is not thread safe by default... In the past I've spent countless hours in the debugger tracking down funny issues because some innocent-looking event handler on the other side of the codebase has been fiddling around with some object's state when I wasn't expecting it. That's a typical symptom of shared mutability and not having a well-formed ownership story.

I also miss the concepts of Send/Sync, value semantics, and explicit unsafe blocks when dealing with raw pointers or other areas you can introduce memory safety issues (C# also has an unsafe keyword, but the presence of IntPtr and safe P/Invoke lets you easily side-step most of its guarantees).

1 Like

Although I've done mostly C# over the past decade or so I've also done some F#, mainly non-commercial. So when I approached Rust I noticed some similarity conceptually and syntactically around immutability and pattern matching. But Rust's pattern matching is more powerful than C#'s (unless I'm missing some advanced F# features).

At the moment it's hard to find an opening for Rust in the typical Microsoft developer job spec. But, since Microsoft is now on board, I foresee some spaces in cloud and web assembly in due course. So with the latter I can imagine, say, high-perf client side AI components in Rust, scripted from JavaScript/TypeScript or even C# Blazor.

That's part of my motivation for learning it, apart from being unemployed right now. :slightly_frowning_face:

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.