TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Getting Past “Ampersand-Driven Development” in Rust

163 点作者 emschwartz大约 2 年前

12 条评论

dathinab大约 2 年前
Just one small content addition:<p>Both `&amp;` and `&amp;mut` can be moved to another thread (through only `&amp;` can be used by multiple threads). This holds even if the livetime is not `&#x27;static`, but requires the usage of a scoped threads API which use a trick to assure that the sub-thread(s) can not outlive the parent thread from which the `&amp;`&#x2F;`&amp;mut` comes.<p>Originally such an API was in rust&#x2F;std but it was removed as it was found to not be sound in the way it was implemented, but sound versions of this API do exist(1) since a long time as libraries and there have been efforts to move it back into std (but I don&#x27;t know the state of it).<p>(1): For example crossbeam::scope : <a href="https:&#x2F;&#x2F;docs.rs&#x2F;crossbeam&#x2F;latest&#x2F;crossbeam&#x2F;fn.scope.html" rel="nofollow">https:&#x2F;&#x2F;docs.rs&#x2F;crossbeam&#x2F;latest&#x2F;crossbeam&#x2F;fn.scope.html</a>
评论 #35081321 未加载
iandanforth大约 2 年前
This is a great post demonstrating just how bad the ergonomics of Rust are (I know I know, that&#x27;s not the point of Rust). But for people who care about language usability you can point to this post and say &quot;they were not thinking about the user journey during the design process, how would you have designed it differently?&quot;<p>Take the first two sections as a perfect example. You decide you want to introduce a single-character control symbol `&amp;` (which is questionable by itself), how do you apply it? In one case you decide that this control symbol indicates that a value is immutable and in another it&#x27;s part of the declaration of a value as explicitly mutable. You&#x27;ve now introduced a semantic contradiction that a learner has to overcome.<p>Now obviously Rust <i>works</i>, and that&#x27;s its point, and people love that enough to overcome all the ergonomic challenges the language puts up. What I can&#x27;t wait for is <i>Pythonic</i> Rust where we have a language that treats humans as first class citiziens but offers the guarantees that Rust does. The only question in my mind for if this will ever happen is if the fully literate programming modality enabled by LLMs will make it a moot point.
评论 #35081897 未加载
评论 #35081769 未加载
评论 #35082217 未加载
评论 #35081900 未加载
评论 #35081932 未加载
评论 #35081969 未加载
评论 #35082492 未加载
评论 #35081845 未加载
评论 #35081830 未加载
评论 #35084112 未加载
评论 #35083974 未加载
评论 #35084669 未加载
评论 #35082145 未加载
评论 #35086242 未加载
评论 #35084643 未加载
评论 #35082581 未加载
评论 #35081831 未加载
评论 #35088998 未加载
kjuulh大约 2 年前
Another step in getting proficient with ampersand (or rather immutable refs), is pulling stuff from enums and structs, in an immutable way. I.e. Option&lt;SomeStruct&gt;, or Result&lt;SomeStruct, ErrorKind&gt;. Using as_ref and friends.<p>There was a great post the other day about the &quot;Register of rust&quot;, and getting to know all the different variants of these features to be proficient and being able to build whatever you want.<p>The same goes for async features as well. Async especially requires that you understand lambdas, borrow checking and so on. As it can be quite tricky to send a value to a tokio::task::spawn!(...) if you don&#x27;t have some mastery over the borrow checker.<p>When you get to taking lambda in a func, that should be async then you begin to get into the weeds of rust, and where some of the nasty compilers errors show up, some of the unhelpful variants, where you are expected to know the intricacies of how lifetimes work, and how rust handles generics vs impl vs enums etc.<p>In the beginning I just recommend using a good old Arc&lt;Mutex&lt;...&gt;&gt; if you need to pass stuff to functions, you can still shoot yourself in the foot, but you can take the optimizations piecemeal.
评论 #35081943 未加载
emschwartz大约 2 年前
A little mental model I used to explain ownership and borrowing to someone who is new to Rust. I hope others find it helpful!
评论 #35081734 未加载
评论 #35081533 未加载
BiteCode_dev大约 2 年前
I don&#x27;t understand, why is &quot;&amp;&quot; not the default and passing ownership something you must explicitly request ?<p>It seems the latter is more common than the former, and that would avoid to have to add &quot;&amp;&quot; everywhere.
评论 #35081836 未加载
评论 #35081709 未加载
评论 #35087772 未加载
评论 #35081745 未加载
评论 #35082095 未加载
评论 #35081850 未加载
alkonaut大约 2 年前
What tripped me up early on (and still does to some extent) isn&#x27;t the ownership of simple values, but when you get to generics. Do I implement the trait for Option&lt;T&gt; <i>and</i> Option&lt;&amp;T&gt;. When will implementations overlap and when will they not? It becomes even trickier when you realize that a generic T can also represent a reference type &amp;&#x27;a mut U or similar. So you can&#x27;t just blindly implement the trait for both T and &amp;T either.<p>Basically: even though I know exactly what the ownership does in the context of &quot;it&#x27;s a clever C&quot; I&#x27;m still tripping over myself when I take the same concepts to be &quot;a clever C++&quot;.
oytis大约 2 年前
Are these images generated by Dall-E or something similar?
评论 #35081479 未加载
评论 #35082912 未加载
doodlesdev大约 2 年前
<a href="https:&#x2F;&#x2F;archive.ph&#x2F;DMvoN" rel="nofollow">https:&#x2F;&#x2F;archive.ph&#x2F;DMvoN</a><p>For those of us who are allergic to JavaScript.
cmrdporcupine大约 2 年前
Re: &amp; references generally, esp to &amp;self; one of the biggest initial temptations and early pitfalls one will fall into with Rust is to apply object-oriented design patterns (from C++ or Java world, etc), and tie a lot of functions&#x2F;behaviours to structs, instead of having free-floating functions.<p>And it&#x27;s this that can quickly get you into trouble with excessive &amp; and &amp;mut references where you don&#x27;t need them, and trouble with borrowing generally. Passing that &amp;self argument around all over the place -- even when you might not need to -- means you end up borrowing or declaring mutability at times when you may not want to. Also leading down the path of ending up resorting to Arc&#x2F;Rc, or Box etc.<p>It&#x27;s one thing I struggled with, moving mostly from C++, but I suspect people coming from a C (not C++) programming background struggle with a lot less as they are used to what I&#x27;d call module-function-level programming, rather than binding functions to classes all over the place.
_visgean大约 2 年前
Good article but whats the reason for all the syntetic meaningless images?
评论 #35081330 未加载
MR4D大约 2 年前
I do not understand why it is<p>&amp;mut var<p>Instead of<p>mut &amp;var<p>Having two version of mut just doesn’t make sense to me. I love rust, but haven’t gotten nearly enough time to use it, and little things like this use up more memory in my brain than it should for such a well thought out system.
评论 #35084872 未加载
评论 #35087953 未加载
philprx大约 2 年前
Please add example for each section ;-) that would help the rust newbie in me :-)<p>(or links to such examples)