35 points · by krischal · 3y ago · 35 comments
You get about 30 or something number of errors in large project. And you debug, one by one, and one by one. It goes down to 30, 29, 25, 24 .. and with a little patience you get it down to 4, 3, 2, and a error. Sometime you can even increase errors, because there is error around error, but it generally goes down just as quick.
Then you correct the last bug, and it then shows `Finished dev in ## seconds`. And, most probably, your code works just as you expected. No memory bugs, no side stuffs. Sure, there can be logical errors, and it can be corrected. But no hard to know errors. The error message tells you how to correct your syntax error. It's a bit opinionated, but it's good and, at the end of day, it guides you to write a decent code. And finally with `cargo fmt`, and your code looks just as compact and pretty as flawlessly it works.
I prefer modern C++20/23 to Rust.
For 98% of tasks Rust is incredible. But modeling those 2% of problems that don't fit neatly into Rusts domain, the level of "unsafe", "PhantomData", and advanced type-system hacks you need feels obscene when I can use some pointers in C++.
Rust also does not have a compelling platform/story baked into the std for low-level asynchronous programming.
High level Mutexes and locks are there, but you're in for a world of hurt if you want to do something like sharded/striped locking, where the actual data being protected isn't directly owned by the lock (see Tokio's "OwnedMutexGuard" for this, thanks to Alex Chi and Victor Yang for telling me about this)
I find the syntactic verbosity of Rust absolutely appalling:
fn fmt(&self, _: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {}
Surely this monstrous code has advantages over C(++) but in no world would I voluntarily write code in this language if other options were viable. I don't get how people prefer this language outside of a very narrow application context where the lasts millisecond of optimization matters.
Thank you for posting this. I had to google and read up some things.
fn fmt(&self, _: &mut Formatter<'_>) -> Result<(), FmtError> {}
But the common practice is to have an alias Result<T> to mean Result<T, whateverErrorWeAreDealingWith>. This doesn't really maps well to doing impls in the Debug trait, since this trait uses an error that is generally different from the error defined by your own application, but with some care, it can be used. Then it becomes: fn fmt(&self, _: &mut Formatter<'_>) -> Result<()> {}
Okay, but what about the whole rest? it's syntactically heavy, but each part of it is very significant.Take the <'_>. It means that the Formatter struct receives a lifetime parameter but you opted to explictly omit it. In previous Rust editions, you would keep it implicit
fn fmt(&self, _: &mut Formatter) -> Result<()> {}
But this was confusing and ambiguous! People looked at it and sometimes didn't know there is a lifetime involved - until it showed up in compiler error messages!Now, there's two elided lifetimes in this snippet: one in the &self and one in the &mut. Rust has clear rules for this case: your snippet means
fn fmt<'a, b>(&'a self, _: &'b mut Formatter<'_>) -> ...
Once you learn the rules (and here are the rules https://doc.rust-lang.org/nomicon/lifetime-elision.html), this makes the syntax slightly lighter. It's not an issue like the other case because if you know the rule, it's not ambiguous.The _ in place of the variable name is there just to not give a name to the formatter. It's actually a catch-all pattern (like one in the end of a match). If you wanted to give a name but disable the unused lint, you would write _formatter instead.
The rest, I can't really remove much sacrificing the semantics. The only superfluous thing I love is the ->, which is much better than the C convention of putting the return type before the function name (IMO).
The <> syntax itself for generic parameters sucks, and it's about the worst thing that Rust inherited from the C++ syntax. The :: also sucks, and it's also from C++ (the turbofish ::<> combines both, and it was Rust's brainchild. https://turbo.fish/ [0]) Actually most C++ influences on the Rust syntax is a disaster. I would much prefer to have a Haskell-inspired syntax for types, so that the type Result<A, Box<C>> would be written Result A (Box C)
[0] there were multiple attempts to remove the turbofish from the syntax. this thread has a discussion of it https://news.ycombinator.com/item?id=26019053 - also see https://www.reddit.com/r/ProgrammingLanguages/comments/fe3yg...
For what it's worth, C++11 supports that syntax:
auto thing(int a, bool b) -> string;I see a lot of manually-written elided lifetimes.
It is already better than C, as WG21 at least acknowledges there are some security issues and has taken multiple approaches to try to fix some of the underlying issues.
However the way defaults are managed, versus the compiler provided frameworks from pre-C++98, are completely the opposite that security minded developers would wish for.
The NSA cybersecurity bill seems to finally have hit home that this needs to change, however it remains to be seen if those at ISO that care about it will be able ot steer the ship around with their paper proposals like P2657R1, P2723R0,P2687R0.
Bjarne keeps adocating the adoption of Core Guidelines and static analyzers improvements, which in a world where clang struggles to catch up, seems to have quite low priority in tooling improvements.
In a way I guess it depends how relevant many key C++ frameworks and workloads (HPC/HFT/games/LLVM/GCC/FPGA/GUI) will evolve and what Rust can manage to take over.
Any system like Rust will have corner cases where it is awful.
The simplest example is doubly linked lists, specifically, and any circular data structure in general.
They are hard in any language to get right, but for all its sweat and pain Rust is no help here. C++ is a better choice.
If you are fiddling with bits, doing low-level concurrency, writing a GC/Memory Manager etc, or have self-referential datastructures like graphs or trees, you're going to have an order of magnitude easier time writing it in C++. (Rust-experts excepted)
Also, the developer experience writing C++ is worse than Rust by miles. Though the tooling (fuzzing, LSP, debugging, sanitizers, etc) is stellar, likewise with Rust.
The real reason is that C++ feels ancient and creaky. Packaging? Nope. Build system? Yeah, set it up yourself. Easy to use build tools? Erm no. Reasonable error messages with templates? Lol.
Macros and templates feel both central to the language and like hacks. Syntax is also borderline unreadable; double ampersands, lambdas all look like a cat walked across the keyboard.
I'm not hating on C++, I have many happy memories of using it, but Rust feels just so much user friendly.
Conan, vcpkg, NuGet, Hunter, CMake Packages,...
Most of them even support binary dependencies, contrary to cargo (currently).
It just happens that many never used any other ML inspired language, so they attribute those capabilities to Rust.
Also if we don't want government spying easily on all our devices (even open source ones), we all have to take safety seriously.
Thus anyone that wants tooling support in NDK and AGDK at the same level as C and C++ have to do the whole integration themselves.
Android and Visual Studio plugins, project templates, mixed language debugging, APIs, JNI bindings generation, ...
No doubt this is eventually come, maybe something for Google IO this year.
Yeah, but the vast majority of my bugs in C projects were logical errors :-/
Logical errors outnumber memory errors at roughly 100:1
Besides, logic errors in Rust don't cause memory errors as knock-on effects. Most of my logic errors in C and C++ codebases ultimately manifested as some form of unsound memory access.
Even unwrap et al are useful, because they show you explicitly where you assume something potentially unsafe is happening.
Mēh! Possibly.
But you can catch logic errors with logic tests.
Catching memory errors is a different problem.