in ,

My Favorite Rust Function, Hacker News


My favorite rust function isstd :: mem :: dropwhich is used to free or deallocate a value, similar tofree ()in C.

Quoting the docs from stdlib, This function is not magic; it is literally defined as

pubfndropT>(_x:T){}

How could a function definition with an empty body ever be useful?

Ownership and lifetimes are Rust’s most unique features and it gives you the predictability and performance of static memory management without any of its safety problems.

Theownership rulesare pretty simple.

  1. Each value in Rust has a variable that’s called its owner.
  2.   

  3. There can only be one owner at a time.
  4.   

  5. When the owner goes out of scope, the value will be dropped.

Calling a function moves the ownership of the arguments and if the called function decides to do nothing with it, the arguments will be dropped – which is Exactly what we wanted!

Now this might seem like a hack, but it really is not. Most languages ​​would either ask the programmers to explicitly callfree ()or implicitly call a magicruntime.deallocate ()within a complex garbage collector.

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.

  

A language that doesn’t affect the way you think about programming is not worth knowing. – Alan J. Perlis

        

Brave Browser
Read More
Payeer

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

Three Candy Stocks With Breakout Potential Ahead of Halloween, Crypto Coins News

Three Candy Stocks With Breakout Potential Ahead of Halloween, Crypto Coins News

‘Remain vigilant to keep dengue at bay, don’t panic’ – Times of India, The Times of India

‘Remain vigilant to keep dengue at bay, don’t panic’ – Times of India, The Times of India