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.

My favorite Rust function

240 pointsby jaseemabidover 5 years ago

15 comments

foopdoopfoopover 5 years ago
Reminds me of Coq&#x27;s definition of `False`:<p>`Inductive False := .`<p>i.e., `False` has no constructors and hence is an empty type.<p>Anyway, this means that for any type `A`, you can construct a function of type `False -&gt; A` because you just do this:<p>`fun (x : False) =&gt; match x with end.`<p>Since `False` has no constructors, a match statement on a value of type `False` has no cases to match on, and you&#x27;re done. (Coq&#x27;s type system requires a case for each constructor of the type of the thing being matched on.) This is why, if you assume something that is false, you can prove anything. :)
评论 #21239004 未加载
unexaminedlifeover 5 years ago
I get the feeling this doesn&#x27;t really get into the meat of what &quot;drop&quot; is. It seems you can&#x27;t really explain why you &quot;love&quot; a function without discussing its purpose. Maybe I&#x27;m wrong, I&#x27;m only really an outsider looking in when it comes to rust, but it does fascinate me as far as its goals. I would go so far as to say that it will be important for systems programmers to know in the not too distant future (if it&#x27;s not already).<p>Isn&#x27;t it really only there in case someone needs to &quot;hook into&quot; the drop functionality before the variable is dropped? Please correct me if I&#x27;m wrong.<p>EDIT: Minor editing to clarify meaning.
评论 #21238282 未加载
评论 #21237603 未加载
holy_cityover 5 years ago
I wouldn&#x27;t say std::mem::drop acts like free at all, it&#x27;s the equivalent of a destructor in C++. Mostly useful when you&#x27;re dealing with manually allocated memory, FFI, implementing an RAII pattern, etc.<p>One cool thing about Drop (and some other cool stuff, like MaybeUninit) is that it makes doing things like allocating&#x2F;freeing in place just like any other Rust code. There may be some unsafe involved, but the syntax is consistent. Whereas in C++ seeing placement new and manually called destructors can raise eyebrows.
dbieberover 5 years ago
I haven&#x27;t used rust, so can you explain this to me:<p>If I do the rust equivalent of:<p><pre><code> def add1(x): return x + 1 x = 1 y = add1(x) z = add1(x) </code></pre> then will x have been deallocated by the first call to add1 and will the second call to add1 fail?<p>[You can ignore the fact that I&#x27;m using numbers and substitute an object if that makes more sense in the context of allocating &#x2F; deallocating memory in rust.]
评论 #21237173 未加载
评论 #21237181 未加载
评论 #21237156 未加载
评论 #21237132 未加载
评论 #21237212 未加载
grenoireover 5 years ago
Wow, elegant. This was probably conceived as an idea during the design phase of the language, it seems <i>right</i>.
评论 #21236996 未加载
lpghatguyover 5 years ago
Sometimes Rust programmers write this same function as a closure, which can be known as the &#x27;toilet closure&#x27;[1]:<p><pre><code> |_| () </code></pre> [1] <a href="https:&#x2F;&#x2F;twitter.com&#x2F;myrrlyn&#x2F;status&#x2F;1156577337204465664" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;myrrlyn&#x2F;status&#x2F;1156577337204465664</a>
hinkleyover 5 years ago
Do variables go out of scope after last use or when the function exits? I could see the former evolving into the language if it’s not already the default behavior.<p>In which case there’s only one situation where I could see this useful, and that’s when you are building a large object to replace an old one.<p>The semantics of<p><pre><code> foo = buildGiantBoject(); </code></pre> In most languages is that foo exists until reassigned. When the object represents a nontrivial amount of memory, and you don’t have fallback behavior that keeps the old data, then you might see something like<p><pre><code> drop(foo); foo = buildGiantBoject(); </code></pre> Most of the rest of the time it’s not worth the hassle.
评论 #21237185 未加载
评论 #21238793 未加载
评论 #21237570 未加载
saagarjhaover 5 years ago
There’s a number of fun C++ ones similar in spirit: std::move, for example.
评论 #21236921 未加载
评论 #21236976 未加载
newacctjhroover 5 years ago
&gt; Now this might seem like a hack, but it really is not. Most languages would either ask the programmers to explicitly call free() or implicitly call a magic runtime.deallocate() within a complex garbage collector.<p>The compiler actually implicitly adds drop glue to all dropped variables!
cztomsikover 5 years ago
For me, rust is still love &amp; hate, even after 1 year of half-time (most of the free time I have) hacking.<p>It&#x27;s a wonderful language but there are still some PITAs. For example you can&#x27;t initialize some const x: SomeStruct with a function call. Also, zero-cost abstraction is likely the biggest bullshit I&#x27;ve ever heard, there is a <i></i>lot<i></i> of cost and there&#x27;s also a lot of waiting for compiler if you&#x27;re using cargo packages.<p>That said, I wouldn&#x27;t rather use C&#x2F;C++&#x2F;Go&#x2F;Reason&#x2F;Ocaml&#x2F;? - that is probably the love part.<p>BTW: I&#x27;ve recently stopped worrying about unsafe and it got a bit better.<p>So my message is probably: - keep your deps shallow, don&#x27;t be afraid to implement something yourself - if you get pissed off, try again later (sometimes try it the rust way, sometimes just do it in an entirely different way)
评论 #21237448 未加载
评论 #21237310 未加载
评论 #21237429 未加载
评论 #21237771 未加载
评论 #21237365 未加载
评论 #21237271 未加载
millstoneover 5 years ago
<p><pre><code> let x = String::from(&quot;abc&quot;); std::mem::drop(&amp;x); std::mem::drop(&amp;x); std::mem::drop(&amp;x); std::mem::drop(&amp;x);</code></pre>
评论 #21237855 未加载
flywithdolpover 5 years ago
can someone elaborate on this passage:<p>The beauty of programming language design is not building the most complex edifice like Scala or making the language unacceptably crippled like Go - but giving the programmer the ability to represent complex ideas elegantly and safely. Rust really shines in that regard.<p>i&#x27;m fairly ignorant on the various differences but my general feeling was that Go is quite useful?
评论 #21238878 未加载
评论 #21238911 未加载
gautamcgoelover 5 years ago
Gotta admit, that really is a cute example. However, I was a bit surprised when the author described Go as &quot;unacceptably crippled.&quot; What is he referring to?
评论 #21237812 未加载
评论 #21237272 未加载
评论 #21237280 未加载
评论 #21237333 未加载
评论 #21237137 未加载
评论 #21237164 未加载
评论 #21237368 未加载
jchwover 5 years ago
&gt; or making the language unacceptably crippled like Go<p>Gotta say, I lost a lot of respect for the author at this point. It’s not like I don’t love Rust - quite the contrary - but if the only takeaway from Go for you is that it is “unacceptably crippled” then I feel you have missed a lot of insight. Go has been one of my languages of choice for over half a decade now, and for good reason.
评论 #21237944 未加载
评论 #21237274 未加载
评论 #21237870 未加载
评论 #21239024 未加载
评论 #21239750 未加载
评论 #21238810 未加载
评论 #21237952 未加载
评论 #21238543 未加载
etxmover 5 years ago
&gt; unacceptably crippled like Go<p>I just spit mezcal on a stranger.
评论 #21237138 未加载