Disclaimer
This post is not to say which programming language is better for what reason. This is just to share my preference, after thinking and experiencing for months, hoping this may help someone having concerns similar to mine.
To C++ or to Rust, that is the question
Yes. That shall be what many programmers, including myself, have for their future technology adoption and career path, especially if you're C++ programmer. I'm also using C++ as my daily driver, and I'm also familiar with "segmentation fault" and "This program has performed an illegal operation and will be closed", both of which mean there are errors in my manual memory management. So Rust caught my eyes to like many others, claiming "this programming language avoids memory corruption without any additional loads-I'm seeing you, garbage collector- by carefully designed language grammar". Yep. That sounds like a magic or charm. I also tried the language, loved the design, ecosystem as well as syntax, and I applied the language to a handful of commerical applications I was expected to develop. Forget CMake and other C++ legacy. No worries for custom move operator for more efficient std::move(). Const is default so that you don't have to worry about changing should-be-immutable values by mistake. Cargo is GOD, borrow check is FUN, and grammar is LOVELY!
But after launching a few commercial and internal applications by myself, I soon felt something doesn't fit to me.
DON'T INSIST on me
Rust is certinaly on mainstream and get backup from many big tech companies. You know them too. Not to mention Mozilla which worked as the incubator, now it is backed by AWS, Microsoft, Google, Facebook, and so many others. People love Rust, its trait(pun intended), and I'm sure that you'll even fall in love with RUST_BACKTRACE=1(maybe).
However, the language is, in my opinion, stubborn. The language insists on specific way of doing if you'd like to do something. The grammar is quite complicated there are a lot of things to consider. Of course I can accomodate myself into the grammar and make myself fit to the language-or, think like the language. However, my instinct, I found out that I don't like that notion, even though in most situation "the Rust way" is reasonable and acceptable and I agree on them too.
There shall be more efficient ways if the restriction is removed and more freedom is given. For example, what shall be the most efficient way to save an unsigned 64 bit integer into SQLite database, which doesn't support unsigned integers at all? If it were PostgreSQL you may consider using bigger integer types(decimal
or numeric
), but it's just our small and cute SQLite without them. In such situation, at least for me, the most efficient way to do is force typecasting unsigned 64 bit integer to signed one and vice versa. You can easily do this on C++ like following: (int64_t)my_awesome_value
and (uint64_t)value_from_sqlite
. But Rust?
Forget it. I don't even want to count how many steps are needed.
I love freedom over rules
I love freedom. Yet still, I agree that you need to limit your behavior to reduce, if you cannot eliminate, any possible errors at least, by design. I also intentionally design some of classes and functions to follow certain rules and procedures so that application will encounter compile time error if not followed. However, I think it shall be developer's choice, rather than language's constraint. During the development with Rust rustc
complained too much on me so that I had to do something extra, which was not a problem in my good old C++ world. For some people they could be life savior or automatic consultant, but for me it sounded like "nagging from your old mom." I wanted to express more on my own way. Thank you for your kind suggestions in the error message, but I don't that that's the most efficient way to do the business.
So, though I loved cargo
over CMake
and being free from "undefined behavior", I took risk of segfaults to do my fun ride again with C++. With C++ compilers, CMake, Ninja, and Neovim with a few lightweight plugins, I'm free and invincible again!
But I'm still (a bit) Rusty
Though I'm fully back to world of C++, I didn't say that I forgot everything I learned from Rust. I use more smart pointers, I prefer references over pointers to avoid any potential to pass nullptr_t
by accidient(so that application will crash) if null pointer is meaningless in the context, got more ideas of better manipulating CMake by reviewing basics of cargo
. Yep. Rust really influenced and changed my programming style to better design in good way, like my good old friend Object Pascal did to me. Like C, C++ also grants programmers more freedom. You're free to do anything if you know what you're doing(tm). But with great power comes great responsibility - this could result in a situation nobody knows why this application crashes. And if it just works fine in debug mode, you just go crazy(argh). Still I love my freedom to do everything in my own way, with good practicves and designs I learned from Rust as I did in the past with Object Pascal and it TObject
.
I'm not sure which language will be more mainstream in the future between these two. Maybe Zig(https://ziglang.org) can be some contender in the future, but not now at least - it could be a good contender at least it shows OOP grammar as simple as Python, internalizing vtable. For C++ and Rust, at least for me Rust is more like "you MUST do this" while C++ is like "you CAN do it also in this way." While one is highly opinionated, the other is unopinionated at all(that is to say, at least for me. your opinions are always welcome). And that may be one of the reasons that I don't like Qt? :D
Maybe C++ is still superset of Rust in some way (it's just "in some way", because there are things unique in Rust language itself. For example, Rust trait can be mimicked with template class and combination of C++ enum and template class can behave like class-associated Rust enum, but C++ doesn't have anything equivalent or similar to borrow checker).
So, that's all from me. Any ideas or comments are welcome. Let's share our thoughts.
Top comments (1)
I'm been coding on a new project with Rust for the last few months. I only knew the basic of the language at this time, but I have a lot of experience with C++ (8 years?).
It's really too soon to decide whether I will never want to go back to C++, or whether I will miss it a lot.
However, I can say that a lot of things are shinning in Rust. You mentioned cargo, especially combined with crates.io to integrate library super easily. I also like how the compiler is paranoid. Sometimes, it makes me super crazy, but when you want to build truly robust software, it's great. You can probably get very good results too with C++ and a lot a compiler options and linter and static analyzer and so on. With Rust, it's much simpler to set up good foundations for your projects.