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.

Arc and Mutex in Rust

119 pointsby bshanksalmost 3 years ago

9 comments

gnulinuxalmost 3 years ago
I want to note here that throughout the internet and HN there is this ongoing wisdom that although Rust is very useful, valuable etc writing code in it is very hard because it constantly feels like you&#x27;re wrestling the compiler.<p>Well, maybe there is some truth to it, but I want to say that having started learning Rust in 2022, I found it&#x27;s multi-threadedness model very refreshing. I thought it was such a delight to write code in, and personally loved it at least one order of magnitude than Go&#x27;s goroutines. It&#x27;s safe, embedded in type system (Send etc), has tons of standard tools (Arc, Mutex, mspci channel etc...) that are hard to shoot your foot with.<p>When people talk about Rust&#x27;s complicated nature, I wish more emphasis is made on how it completely eliminates entire categories of bugs <i>even within a multi-threaded context</i>. As a corrolary, it really makes some parts of programming hard in other languages, nearly trivial in Rust.
评论 #31641349 未加载
评论 #31641347 未加载
评论 #31641173 未加载
评论 #31641270 未加载
评论 #31641179 未加载
评论 #31644332 未加载
评论 #31641975 未加载
评论 #31642097 未加载
评论 #31647122 未加载
评论 #31648810 未加载
pornelalmost 3 years ago
Very good article, and these types are essential to have in your Rust &quot;vocabulary&quot;.<p>I&#x27;ve seen programmers fight the borrow checker a fight they can&#x27;t win, because `Arc&lt;Mutex&lt;Object&gt;&gt;` has a non-zero overhead and looks uglier than a plain `Object` or `&amp;Object`. However, if the program has object lifetimes that aren&#x27;t statically known and&#x2F;or shared mutable state, these are simply the correct types to use (e.g. event-based systems that can attach listeners have to use these). Rust is just being very explicit about ownership and putting all overheads in your face. In something like Python there&#x27;s no extra syntax for this, because every object is automatically under a lock and has a refcount.
vgelalmost 3 years ago
One option the author didn&#x27;t mention is that threads can join with a value, so instead of needing scope it could have looked like (on my phone so syntax is probably not quite right):<p><pre><code> let user = todo!(); let handle: JoinHandle&lt;User&gt; = spawn(move || { &#x2F;&#x2F; do stuff with user &#x2F;&#x2F; hand back user user }); &#x2F;&#x2F; wait for thread to stop and retake ownership of user let user = handle.join(); </code></pre> I used this recently to remove locking in a worker thread and it was a big performance boost, <i>and</i> made the code nicer -- win&#x2F;win.
Communitivityalmost 3 years ago
Every language is hard when you are working with hard problems (e.g. concurrency).<p>I&#x27;ve also found Rust to be hard until I got a decent understanding of the borrow checker and how it works.<p>After that, the only times I found Rust to be hard were when I was trying to make it conform to my way of thinking, rather than changing how I approached the problem to match the way Rust was designed. I&#x27;ve had a similar experience in other complex and well-designed languages such as Clojure, Erlang, and Haskell.<p>In my experience the benefits of Rust (safety-guarantees, speed, good ease of use vs complexity tradeoffs) much outweigh the quirks and learning curve.
lenkitealmost 3 years ago
A Rust newbie here. Can someone explain why rustc complains that the clojure may outlive the function ? I mean we are joining on both thread handles so the main function has to block until they finish right ? Shouldn&#x27;t RAII get rid of the clojure objects ?
评论 #31643873 未加载
hintymadalmost 3 years ago
I think a programmer can get over the &quot;Rust is hard&quot; part after writing certain amount of code. The type system and the borrow checker will just become intuitive enough for a programmer to grasp. I do have a question about productivity, though: what added productivity does Rust bring over languages like Go or Java for writing RPC&#x2F;HTTP services, given that Rust requires an engineer to think about memory management all the time, while GC languages don&#x27;t?
singhracalmost 3 years ago
I really liked this article, but if I can provide some sort of drive-by advice (also for others looking to write technical content), providing some examples of where to <i>use</i> various traits would really help, e.g. what kind of data structure one would implement !Send on.
评论 #31645245 未加载
ostenningalmost 3 years ago
Theres a lot of criticism here about “async rust”. I find this wording strange, I use rust asynchronously with rtic (realtime interrupt concurrency) for low level work. Its really excellent. Are peoples criticisms specifically about inline async?
pipeline_peakalmost 3 years ago
Return type definitions are optional as Rust programmer&#x27;s might get confused by difficult words like &quot;void&quot;.
评论 #31643125 未加载