There are so many different flavours of C++ put there that this guide doesn’t exactly do itself the credit it deserves.<p>There are easy ways to implement stuff like enums with members in C++, just put an anonymous enum inside a class/struct, its possible but marked as not possible.<p>Likewise when discussing modules in rust while completely ignoring the existence of modules in C++ that is actually support by modern tooling.<p>There are other places where there are similar issues I have with the text (you can just add a compiler flag and get the same behaviour. Don’t even try and argue “but rust just does it out of the box”, I would need to rewrite my entire codebase vs just adding a few bits of text to my build system, these things are not equivalent)<p>They didn’t discuss much about FFI at all, generally sayings “theres a crate for that and if there isn’t let us know”, in my experience the crates are not amazing for esoteric things (anything graphics’s related, ffmpeg is another one) and are actually significantly more painful to use that just writing in a restricted version of C++.<p>Rust has a happy path, and they are broadening it incrementally, but theres is a lifetime of history before rust even existed in C++ that isn’t that easy to sweep under the rug.
One of the common pitfalls I've seen in my time is someone writing a language they are familiar with in a language that just doesn't fit; trying to apply idioms that flow well with one language to another language where that's just not a good way to achieve the same ends.<p>An example I've seen a lot is a C thinker writing C++ classes with an init() function; sure, it works, but the C++ way is to do that in constructors. (All those about to start listing exceptions to that C++ idiom, please save it to the end, thanks!) The C thinker is still thinking about objects as "allocate memory, then set values" rather than the C++ way where allocation and initialisation are wrapped together into a single action (from the programmer's point of view).<p>So what are these pitfalls for a C++ thinker when writing Rust? This "phrasebook" is embracing the idea of taking a C++ way of thinking and applying it to Rust, which I'm sure will be fine for many situations, but what are the C++ phrases that are just the wrong way to do things in Rust?
The discussion of traits vs. classes glosses over a major difference - Rust traits have no associated data, and you cannot access the data of a parent trait. Trying to do object-oriented programming in Rust quickly leads to a mess. This is a huge problem if you try to write C++ in Rust.<p>There's no mention of ownership at all.<p>> Many libraries in Rust will offer two versions of an API, one which returns a Result or Option type and one of which panics, so that the interpretation of the error (expected exceptional case or programmer bug) can be chosen by the caller.<p>Huh? Usually, in Rust you just put .unwrap() or .expect() on a function call that returns a result if you want a panic.<p>More generally, most of the differences between Rust and C++ relate not on how to write imperative code, but how to lay out connections between data structures. Most hard problems in Rust relate to ownership design. That's true in C++, too, but you don't discover them until the program crashes at run time.
been awhile sincei looked but i thought Drysdale's Effective Rust was a truly excellent book and aimed at simlar audience<p><a href="https://www.lurklurk.org/effective-rust/" rel="nofollow">https://www.lurklurk.org/effective-rust/</a>