Can a function with no body be useful, yes!

 — 1 minute read


Jaseem Abid describes his favorite Rust function std::mem::drop

My favorite rust function is std::mem::drop which is used to free or deallocate a value, similar to free() in C.

pub fn drop<T>(_x: T) { }

The reason this function actually has a purpose is because of Rust’s ownership model:

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!

Jaseem closes his post with a quote from Alan J. Perlis:

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

I very much like this idea.