TE
TechEcho
AccueilTop 24hRécentsMeilleursQuestionsPrésentationsEmplois
GitHubTwitter
Accueil

TechEcho

Une plateforme d'actualités technologiques construite avec Next.js, fournissant des nouvelles et discussions technologiques mondiales.

GitHubTwitter

Accueil

AccueilRécentsMeilleursQuestionsPrésentationsEmplois

Ressources

HackerNews APIHackerNews OriginalNext.js

© 2025 TechEcho. Tous droits réservés.

C++ to Rust Phrasebook

216 pointspar wcrichtonil y a 6 jours

9 comments

jpc0il y a 5 jours
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&#x2F;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.
评论 #44143795 未加载
评论 #44144136 未加载
评论 #44143603 未加载
评论 #44148123 未加载
EliRiversil y a 5 jours
One of the common pitfalls I&#x27;ve seen in my time is someone writing a language they are familiar with in a language that just doesn&#x27;t fit; trying to apply idioms that flow well with one language to another language where that&#x27;s just not a good way to achieve the same ends.<p>An example I&#x27;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 &quot;allocate memory, then set values&quot; rather than the C++ way where allocation and initialisation are wrapped together into a single action (from the programmer&#x27;s point of view).<p>So what are these pitfalls for a C++ thinker when writing Rust? This &quot;phrasebook&quot; is embracing the idea of taking a C++ way of thinking and applying it to Rust, which I&#x27;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?
评论 #44143730 未加载
评论 #44144912 未加载
评论 #44143821 未加载
评论 #44143021 未加载
评论 #44144662 未加载
ameliusil y a 5 jours
I noticed that for most of the examples the Rust version is more verbose.
评论 #44144756 未加载
评论 #44146122 未加载
Animatsil y a 5 jours
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&#x27;s no mention of ownership at all.<p>&gt; 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&#x27;s true in C++, too, but you don&#x27;t discover them until the program crashes at run time.
npalliil y a 5 jours
Inverting the original intent, this is great, learn how Rust improves certain things and use that to write better C++ if possible.
评论 #44143549 未加载
90s_devil y a 5 jours
I will be using this to learn both Rust and C++ and see which one I like better.
评论 #44143293 未加载
评论 #44144535 未加载
leohil y a 5 jours
Epic. This is really good.
gtaniil y a 4 jours
been awhile sincei looked but i thought Drysdale&#x27;s Effective Rust was a truly excellent book and aimed at simlar audience<p><a href="https:&#x2F;&#x2F;www.lurklurk.org&#x2F;effective-rust&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.lurklurk.org&#x2F;effective-rust&#x2F;</a>
unvalleyil y a 4 jours
Nice, it also helps me like a Rust developer that has no C++ experience.