r/cpp • u/isht_0x37 • Sep 04 '23
Considering C++ over Rust.
To give a brief intro, I have worked with both Rust and C++. Rust mainly for web servers plus CLI tools, and C++ for game development (Unreal Engine) and writing UE plugins.
Recently one of my friend, who's a Javascript dev said to me in a conversation, "why are you using C++, it's bad and Rust fixes all the issues C++ has". That's one of the major slogan Rust community has been using. And to be fair, that's none of the reasons I started using Rust for - it was the ease of using a standard package manager, cargo. One more reason being the creator of Node saying "I won't ever start a new C++ project again in my life" on his talk about Deno (the Node.js successor written in Rust)
On the other hand, I've been working with C++ for years, heavily with Unreal Engine, and I have never in my life faced an issue that usually the rust community lists. There are smart pointers, and I feel like modern C++ fixes a lot of issues that are being addressed as weak points of C++. I think, it mainly depends on what kind of programmer you are, and how experienced you are in it.
I wanted to ask the people at r/cpp, what is your take on this? Did you try Rust? What's the reason you still prefer using C++ over rust. Or did you eventually move away from C++?
Kind of curious.
4
u/kouteiheika Sep 07 '23
I'm someone who switched to Rust right around version 1.0 dropped, and I didn't do it for memory safety. In fact, I didn't care about memory safety at all, and just wanted a more convenient/ergonomic/productive C++.
So let me give you my subjective laundry list of things why I use Rust over C++ (this is purely a subjective list, your mileage my vary, I'm not judging which language is inherently "better" here, just writing out why I like it). You might notice that some (most?) of them might not be that big of a deal in isolation, but to me that's more of a death by a thousand papercuts situation, and what makes it particularly appealing for me is the sum of all of them.
Easy dependency management. Do I want to use SDL2, OpenSSL, jemalloc, libfreetype and LuaJIT in my program? (Notice they're all non-Rust dependencies.) I just spend the 30 seconds to
cargo addthem and... I'm good to go. And out-of-box it works and builds cross-platform on Linux, Windows and macOS (which is mostly all I care about).I can easily cross-compile, build and test my project on multiple architectures. Want to, say, run my tests on MIPS? Easy. Install cross and just run
cross test --target mips64-unknown-linux-gnuabi64.I can easily browse through the API of all of my dependencies. I just run
cargo doc --openand a browser window pops up where I can search through/read the docs for every dependency I have, without having to hunt down the docs separately or manually search for their header files/source code and read that.The default hash map in the standard library has (mostly) state-of-art performance.
The mutex in the standard library is implemented directly using OS-specific APIs, so it's both smaller and faster than pthreads.
All of the APIs support Unicode by default and use UTF-8. In particular, on Windows opening files just works without having to transcode to UTF-16 and use Windows-specific APIs. (Without limiting yourself to the most recent versions where IIRC you can finally enable UTF-8.)
Easy print-style debugging. I just annotate my struct/class with
#[derive(Debug)]and I can print them out.No need to forward declare anything; definition order doesn't matter.
No header files.
No need to move something to a header file to tell the compiler that I want it inlined. (You just slap a
#[inline]or#[inline(always)]on it.)No exception-based error handling.
Constructors are always named, and can fail without throwing an exception.
Less boilerplate (e.g. no rule of five).
Existing classes can be extended with new methods from the outside, just as if the method was defined on the class itself.
Accessing member fields is always prefixed with
self.so it's easy to grep for.No implicit accidental copying. Deep copies are explicit.
Useless memory allocations are essentially never triggered when passing strings and vectors (because everything idiomatically uses Rust's equivalent to
string_view)Strings can be cheaply sliced because they're not zero terminated.
Tests can be defined in the same file as the source code, and can access all of the private fields and methods, so there's essentially never the need to export anything just for tests.
No need to deal with cmake.
Iterators are really easy and convenient to define.
No implicit casts when doing arithmetic.
restricteverywhere by default (on some of my programs this, in extreme cases, this can give ~15% better performance; I checked by compiling and benchmarking the same program with and without this)Inline assembly is part of the language, and its so much nicer to use.
Less cognitive overhead due to less footguns. (There are still some though.)
First class support for a zero-sized unit type that gets properly optimized down. (e.g. if you add an element to
Vec<()>then it doesn't even allocate any memory)include_bytes!to easily include a file as an array.Niche optimization by default (e.g. a reference inside of an
Option[std::optional] doesn't consume any extra space, because the compiler knows that a reference can never be null, so it uses that to represent the tag of the underlying sum type)You can actually use references inside of an
OptionStructs are by default reordered to minimize wasted memory due to padding.
More minimal and convenient lambda syntax.
I don't have to guess what magic
#ifdefmacro soup I have to add to check for a given platform. Want to use a certain piece of code only on Windows? Mark it with#[cfg(windows)]. Only on 32-bit RISC-V? Mark it with#[cfg(target_arch = "riscv32")]. All of these are "standardized" and documented by the compiler....and of course all of the usual suspects that people usually list (pattern matching, sum types, cargo, no data races, memory safety, etc.)
And the issue people have with Rust, namely having to fight the borrow checker, is just not an issue at all for an experienced Rust programmer (at least in my experience). I lose maybe 1% of my productivity to it, at worst.
Can you have most of these things in C++, if you invest enough time and effort? Uh, yes, sure, but why would I want to? Again, for me it's not necessarily that you can't fundamentally have these things in C++, but that it's just a lot more work, and I'm lazy by nature. For the kinds of programs I write the question is not "why would I use Rust?" but "why would I use C++?".
(Again, this is purely subjective; if you like and prefer C++ then keep on using it.)