Hacker News new | ask | show | jobs
by RaycatRakittra 2541 days ago
If someone inexperienced in systems programming chose Rust as their first systems language, would there be difficulty adapting to other languages like C++? It seems like I'm caught in this back and forth between "C++ isn't pretty but it makes you money" and "Rust is so nice but where are the jobs".
4 comments

Are you trying to become a systems programmer? Then you need to know C and C++, period. And not just C and C++, but also the surrounding ecosystem like CMake, Make, Autoconf, GoogleTest, Catch2, Python, Qt5, Boost, etc. Unless you are fortunate enough to work at a place doing completely new development, the programs you will work on are written in C/C++. Knowing a language isn't just knowing the syntax, but also the libraries, scripting, and build tools.

If this is for fun / education then learn Rust. It's conceptually nicer and doesn't have legacy cruft from decades of industrial use.

On the contrary, I'd say that learning Rust is a fabulous stepping stone to "modern" C++ (much of which served as the philosophical foundation for Rust in the first place). And once you get good enough at Rust that you've internalized the rules regarding memory ownership, you'll be able to instinctively apply those same rules successfully in C++, where the compiler proves fewer things for you.
I appreciate what you're saying, and that there's some truth to that, but I think there's at least two components to good allocation management in C++ (or C, or other memory-unsafe language)...

The first component is conventions and idioms for managing allocations, and Rust will force you into (and support) some good (but nontrivial) ones.

The second component is self-discipline. Look at the long history of vulnerabilities in C and C++ code that are due to carelessness -- of an oops that a programmer made when they knew better.

If what's being considered is Rust as a stepping stone to C++, how much does Rust help with the first component, and is Rust even counterproductive for the second component?

Regarding counterproductive for the second component, you might've seen a conventional practice of grinding the Rust Clippy until the code compiles. I don't know how that affects the development of self-discipline (e.g., maybe some people try to make a practice of being Clippy-free on every compile attempt?), but it seems a reasonable and interesting question to ask.

(I'm not dissing Rust for this. I mostly like Rust, and would be happy to be working in/on it.)

I'd say that concerns regarding self-discipline are overblown in this case. Experienced Rust programmers aren't simply typing blindly into their editor and hoping that their code will compile. When writing Rust one comes to learn the code that the compiler likes, and strives to write code that is free of compiler errors in the first place. This is itself an expression of self-discipline, except that the discipline comes in the form of compiler errors rather than runtime errors. There's less of a penalty for making an error in Rust than in C++, but that's going to be true regardless of whether one's background is "I already know Rust" or "I don't already know Rust", which is what the parent commenter appears to be concerned about.
> When writing Rust one comes to learn the code that the compiler likes, and strives to write code that is free of compiler errors in the first place.

I suggested that possibility, but is it generally true, or something personally true for you, or are you advocating that it would be good if people did that?

When I started learning Rust and low-level programming (I'm coming from Ruby), spamming/searching for error/fixing code and wait for `cargo build` to turn green was the strategy I used. As I have more experience with Rust I become more and more aware of ownership/lifetime of everything I'm using, the compiler errors appear less and less, most of the time it's typo or mut missing now and not lifetime issues anymore.

So yes, if you work enough with the borrow checker, your brain will form another logical one, and that one you can use for writing C/C++ code. I have much more confident now in learning/writing C/C++ than before I learn Rust, because I feel like I can form a Rust-like design (tree-based, clear ownership/lifetime objects) and put that in using C/C++ syntax.

Definitely recommend using Rust as stepping stone to learn production-grade level C/C++.

I personally think it’s more nuanced than either of these. I do blindly type stuff in, and lean on the compiler to help me. At the same time, as I got more proficient, my initial code is closer to correct and produces far less errors.
Adapting won't be too hard. I've heard a few people say learning rust made them better C++ coders, since the habits which make it easier to write rust which passes the borrow checker also make for more robust C++ code (in effect, the same rules the rust compiler enforces also apply to C++ code, but the compiler cannot help you nearly as much).
The main difficulty is that the compiler no longer checks your work.

There are of course, fairly significant differences in idioms, and things like that, but that’s true for every language switch.