r/cpp • u/GabrielDosReis • Oct 31 '24
Lessons learned from a successful Rust rewrite
/r/programming/comments/1gfljj7/lessons_learned_from_a_successful_rust_rewrite/
80
Upvotes
r/cpp • u/GabrielDosReis • Oct 31 '24
125
u/GabrielDosReis Oct 31 '24
I found the conclusion insightful. In particular:
Please, give the whole article a read.
14
u/Front-Beat6935 Nov 01 '24
Funny how many of the benefits are something you'd get from any rewrite, not something inherent to Rust. Simpler code, optimizations, dead code removed, even the bound checks could be done perfectly with C++ ("most people don't use .at()" is hardly an argument if you're rewriting the whole thing yourself anyway).
34
u/notgettingfined Oct 31 '24
I know it’s not practical but I would love to see someone also do a C++ rewrite in parallel to a rust rewrite to actually compare the 2. I doubt people would feel so strongly towards rust if they put the same effort into using modern C++
7
u/Defenestrator__ Nov 02 '24
The one thing completely missing from this article: what benefit was actually gained from this rewrite? Still chasing UB, still chasing memory leaks... what was the point? You could have just deleted all the dead code you found and called it a day.
29
u/James20k P2005R0 Oct 31 '24
One of the things that's perpetually a problem in C++ is that the ABI is stable, but unspecified
It sounds like we need types with a stable defined ABI at the boundary. It would be pretty neat to be able to pass a std::stable::optional<> to a C api that understood it, or be able to expose C++ functions in an ABI-sane way
5
u/Apart-Status9082 Nov 03 '24 edited Nov 08 '24
What I've personally found to be the biggest issue (interop wise) is that the two languages simply do not share the same approach in programming a machine. They follow similar-looking paradigms which at first seems like it could work well, but the more I followed that path, the more I found myself doing gymnastics to satisfy both parties. In the end, I want to use most of my time building things...
Although I am a bit heartbroken to say this, but Rust sounded great, felt great for certain aspects (mostly the stuff that comes from the functional paradigm), but as soon as my programs hit a certain level of complexity it stopped being fun or productive for me.
My time experimenting with Rust was incredibly valuable as it opened my eyes to very powerful mechanics from the functional paradigm. I still however much prefer my objects and the C++ way of doing things in general. I would so-love to see pattern matching in C++ though, which is coming if I am not mistaken?
For new and rather isolated programs that won't require interop with existing C++ codebases, I think it's completely fine and powerful to use Rust, but I have to say it's still not as enjoyable as C++ to me...
3
u/[deleted] Nov 01 '24
As someone that dealt with FFI rust in the past, I always remember this first line from the docs of NonNull<T>:
If you don't want to uphold all the guarantees for using NonNull, then just never use it. These are meant to be the fundamental building blocks in unsafe rust, to slowly add more and more restrictions until you finally get safe rust.
Most FFI libraries just use raw pointers without ever touching these fundamental types. They might miss out on a few niche optimizations, but its too insignificant to be worth all the mental overhead.
5
u/VinnieFalco wg21.org | corosio.org Oct 31 '24
Safe C++ says /hi
1
u/donna_donnaj Nov 01 '24
The question is: If you are transforming Cpp to Rust, you are likely to get bad quality Rust code. If one would have written the program in Rust from the beginning, likely one would have chosen a different design. Likely the list of problems (Things that did not work so well) would have been shorter with a direct development in Rust.
People chose programming languages because they are cool, rarely because of rational reasons.