TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Lessons learned from a successful Rust rewrite

146 pointsby broken_broken_7 months ago

12 comments

steveklabnik7 months ago
Incidentally, the first code sample <i>can</i> work, you just need to use the new raw syntax, or addr_of_mut on older Rusts:<p><pre><code> fn main() { let mut x = 1; unsafe { let a = &amp;raw mut x; let b = &amp;raw mut x; *a = 2; *b = 3; } } </code></pre> The issue is that the way that the code was before, you&#x27;d be creating a temporary &amp;mut T to a location where a pointer already exists. This new syntax gives you a way to create a *mut T without the intermediate &amp;mut T.<p>That said, this doesn&#x27;t mean that the pain is invalid; unsafe Rust is tricky. But at least in this case, the fix isn&#x27;t too bad.
评论 #41997864 未加载
ubj7 months ago
I&#x27;ve recently seen a lot of Rust rewrite projects that have talked about how much they&#x27;ve been required to use unsafe blocks. I&#x27;m currently in process of my first C++-to-Rust rewrite, and I haven&#x27;t needed to reach for unsafe at all yet.<p>What kinds of projects or C++ features are requiring such high usage of unsafe? I&#x27;m not implying that this is bad or unnecessary--I&#x27;m genuinely curious as to what requires unsafe to be used so frequently. Since by all accounts unsafe Rust can be harder to use than C++, this may help inform me as to whether I attempt using Rust in future rewrites.
评论 #41997712 未加载
评论 #41997704 未加载
评论 #42006594 未加载
WhatIsDukkha7 months ago
This seems like a weird use of Rust.<p>There is no mention of how much of the codebase is even in safe Rust after all this work so no clear value to the migration?<p>Frequently when people get their code ported they then begin a process of reducing the unsafe surface area but not here.<p>The author seems to have little or no value on safe Rust? It doesn&#x27;t seem evident from reading&#x2F;skimming his 4 articles on the process.<p>Interesting mechanical bits to read for sure though so it&#x27; still a useful read more broadly.<p>It&#x27;s unsurprising that the author would go use Zig next time since they didn&#x27;t seem to have any value alignment with Rust&#x27;s core safety guarantees.
评论 #42001348 未加载
评论 #41999077 未加载
评论 #41998904 未加载
mmastrac7 months ago
This post is subtly wrong: &quot;multiple read-only pointers XOR one mutable pointer&quot; is actually &quot;multiple read-only references XOR one mutable reference&quot;.<p>It _is_ valid to have multiple mutable pointers, just as C and C++ allow. It&#x27;s when you have multiple live, mutable references (including pointers created from live mutable references) that you end up in UB territory.
评论 #41999351 未加载
评论 #42011875 未加载
hyperman17 months ago
As someone who likes what Rust brings to the table, I am pleasantly surprised with the honesty of this review.<p>Interfacing with the C world, both as caller and calllee, happens a lot in real world code. All the C bugs come right back at that point.
pjmlp7 months ago
&gt; Many, many hours of hair pulling would be avoided if Rust and C++ adopted, like C, a stable ABI.<p>What people mistakenly take for the C ABI, is in reality the OS ABI when written in C.<p>Two C binary libraries might fail to link, or reveal strange behaviours&#x2F;crashes, when compiled with different C compilers, for anything that isn&#x27;t clearly defined as part of the OS ABI.
showsomerespect7 months ago
The &quot;arena allocator&quot; hyperlink links to localhost:8000
kelnos7 months ago
I feel like some of the &quot;what didn&#x27;t go so well&quot; sections were essentially because their rewrite was incomplete:<p>&gt; <i>I am still chasing Undefined Behavior. Doing an incremental rewrite from C&#x2F;C++ to Rust, we had to use a lot of raw pointers and unsafe{} blocks. And even when segregating these to the entry point of the library, they proved to be a big pain in the neck.</i><p>These sound like an artifact of the rewrite itself, and I suspect many of these unsafe blocks can be rewritten safely now that there is no C++ code left.<p>&gt; <i>I am talking about code that Miri cannot run, period: [some code that calls OpenSSL (mbedtls?) directly]</i><p>This should be replaced by a safe OpenSSL (mbedtls?) wrapper, or if it wouldn&#x27;t change the behavior of their library in incompatible ways, rustls.<p>&gt; <i>I am still chasing memory leaks. Our library offers a C API, something like this: [init()&#x2F;release() C memory management pattern]</i><p>Not sure what this has to do with Rust, though. Yes, if you&#x27;re going to test your library using the exposed C API interface, your tests may have memory leaks. And yes, if your users are expected to use the library using the C API, they will have to be just as careful about memory as they were before.<p>The benefit of this rewrite in Rust would be about them not misusing memory <i>internally</i> inside the library. If that benefit isn&#x27;t useful enough, then they shouldn&#x27;t have done this rewrite.<p>&gt; <i>Cross-compilation does not always work</i><p>I&#x27;ve certainly run into issues with cross-compilation with Rust, but it is always <i>so</i> much easier than with C&#x2F;C++.<p>&gt; <i>Cbindgen does not always work. [...] Every time, I thought of dumping cbindgen and writing all of the C prototypes by hand. I think it would have been simpler in the end.</i><p>I&#x27;m skeptical of the idea that an automated tool is going to generate something that you&#x27;ll want to use as your public API. I would probably use cbindgen to get a first draft of the API, modify and clean up the output, and use that as the first version, and then manually add&#x2F;change things from there as the API changes.<p>I don&#x27;t want to silently, accidentally change the API (or worse, ABI) of my library because a code generator changed behavior in a subtle way based on either me upgrading it, or me changing my code in a seemingly-innocuous way.<p>&gt; <i>Unstable ABI</i><p>This is a bummer, but consider that they are not exposing a Rust API to their customers: they&#x27;re exposing a C API. Why would the expect to be able to expose Rust types through the API?<p>And they actually <i>can</i> do this: while it is correct that standard Rust types could have a different layout depending on what version of rustc is used to build it, that doesn&#x27;t actually matter for a pre-built, distributed binary, as long as access to those types from the outside code (that is, through the C API) is done only through accessors&#x2F;functions and never through direct struct member access. Sure, that requires some overhead, but I would argue that you should never expose struct&#x2F;object internals in your public API anyway.
IshKebab7 months ago
Hmm yeah I&#x27;m not surprised that interfacing safe Rust with an existing unsafe C&#x2F;C++ API is painful. That&#x27;s really true in every language. (Although I haven&#x27;t tried Zig tbf.)<p>I&#x27;m also not totally convinced that rewrite from scratch is <i>always</i> the wrong thing. For small projects the total work rewriting can be <i>much</i> less than dealing with this kind of FFI.
happyweasel7 months ago
The only real comparison would be a rewrite in modern c++ and then compare that to the rewrite in rust. Also the author mentioned that the original code had no tests at all. Well, good luck.
评论 #41999117 未加载
nesarkvechnep7 months ago
What do we expect from someone who says C&#x2F;C++?
tharne7 months ago
That&#x27;s a confusing title. I was under the impression that on Hacker news, every Rust rewrite is a successful rewrite.
评论 #41998960 未加载