News for hackers. Stuff that matters.
Re:Rust means never having to close a socket by monocasa on Tuesday October 07, 2014 @12:54PM (#8422275)
> Wherever you would have written "socket.close()", in rust you can just say "drop(socket)". The difference is, it takes advantage of the ownership system to statically prove that you won't try to use the socket after close.

Does a drop somehow say something to the type system as well, disallowing reads and writes afterwards? I'm new to rust, sorry if that's a stupid question.

Re:Rust means never having to close a socket by kibwen on Tuesday October 07, 2014 @01:20PM (#8422462)
In fact, the `drop` function is totally trivial. It's just a function that takes ownership of a thing, then does nothing with it whatsoever. In other words, the function body is entirely empty! :)

https://github.com/rust-lang/rust/blob/master/src/libcore/me...

Re:Rust means never having to close a socket by portmanteaufu on Tuesday October 07, 2014 @03:20PM (#8423355)
Ha! I've been using this function for ages and always assumed it was a compiler built-in. I love that it's just that simple.
Re:Rust means never having to close a socket by Double_Cast on Tuesday October 07, 2014 @11:02PM (#8425133)
That's just too perfect. Of all things, your comment has raised my opinion of Rust the most.
Re:Rust means never having to close a socket by pcwalton on Tuesday October 07, 2014 @12:55PM (#8422279)
> Does a drop somehow say something to the type system as well, disallowing reads and writes afterwards?

Yes.

Re:Rust means never having to close a socket by msopena on Tuesday October 07, 2014 @01:39PM (#8422600)
I'm curious about the drop implementation, which I found here (http://doc.rust-lang.org/src/core/home/rustbuild/src/rust-bu...):

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

I understand that the drop function is simply taking ownership of the passed value so Rust knows that once "drop" finishes, the _x can be 'dropped'.

But I though that the T had to be bound to have "Drop" trait (i.e. T: Drop) so that Rust knows it's possible to insert the call to 'drop' like _x.drop()?

Re:Rust means never having to close a socket by kibwen on Tuesday October 07, 2014 @02:07PM (#8422839)
The names are a bit confusing here, and I might petition to change them.

The `drop` here is just a simple library function and isn't technically related to the `Drop` trait, which is implemented with magical compiler pixie dust and allows you to define a destructor via a `.drop` method. Anything that goes out of scope has this `.drop` method called automagically.

You're correct in that if you were calling `x.drop()` explicitly, you would need to have a `Drop` bound on `T`.

Re:Rust means never having to close a socket by ef4 on Tuesday October 07, 2014 @01:56PM (#8422761)
That's a good question.

If dropping was really implemented that way, every type would need to implement the Drop trait. Because otherwise there would be no way to free any memory at all.

Instead, Rust knows how to free any object. Implementing Drop is optional, and you only need to do it if you want some custom behavior at destruction.

Re:Rust means never having to close a socket by hrjet on Tuesday October 07, 2014 @01:24PM (#8422489)
Curious to know why `drop` isn't called automatically by the compiler as soon as possible (since the type system already seems to know it is safe).

Is there a trade-off involved here?

Re:Rust means never having to close a socket by kibwen on Tuesday October 07, 2014 @01:34PM (#8422568)
Freeing resources as soon as humanly (computerly?) possible will optimize your memory usage, but can actually be worse for execution speed. If you're willing to use slightly more memory, it's generally faster to free memory in coarse chunks than one-thing-at-a-time. So even though Rust can conceivably free memory sooner, it might as well defer freeing until the end of the scope, since it can prove that that's safe.

To take this to the logical extreme, see this awesome post from Walter Bright, where he doubles the speed of the D compiler by providing a custom malloc implementation that never frees, leaking all memory until the end of the process: http://www.drdobbs.com/cpp/increasing-compiler-speed-by-over...

Re:Rust means never having to close a socket by hrjet on Tuesday October 07, 2014 @11:14PM (#8425165)
Thanks. I can see the pro in that choice. But the con would be memory starvation (assuming that heap is bounded as in the JVM).

I think one middle-path could be to mark the resource as released as soon as compiler detects it, and then release it

1. when the allocator is starved or

2. when the resource goes out of scope

3. when drop() is explicitly called

Re:Rust means never having to close a socket by kibwen on Wednesday October 08, 2014 @10:18AM (#8426967)
Heaps aren't bounded in systems contexts; a process is free to gobble up the entire address space if left unchecked, which would (hopefully) result in your process being killed by the OS.

I believe we have discussed your first bullet point there, with regard to attempting to free waiting-for-the-scope-to-end-but-not-being-used memory in the event that allocation fails. But this is a hairy subject, and is beyond my expertise.

Note that your third bullet point does cause memory to be freed immediately, because the `drop` function is a scope all its own.

Re:Rust means never having to close a socket by ben0x539 on Tuesday October 07, 2014 @01:40PM (#8422617)
One reason is probably emulating the way RAII works in C++, and allowing "low level" code to use idioms like

    {
        let _lock = acquire_lock();
        
        use_resource();
    } // lock is released here, whether use_resource() returned or caused stack unwinding
Re:Rust means never having to close a socket by hrjet on Wednesday October 08, 2014 @02:05AM (#8425517)
Makes a lot of sense: reduces generated code duplication. Just needs one "release" block per scope.
Re:Rust means never having to close a socket by Leon on Tuesday October 07, 2014 @01:16PM (#8422432)
The other poster says it proves operations won't happen again - does it do both?
Re:Rust means never having to close a socket by kibwen on Tuesday October 07, 2014 @01:25PM (#8422499)
Yep! The owner of a thing can do the following:

1. Give ownership away to something else.

2. Free the object at the end of the scope.

In order to do anything to the object, you give up ownership to the function that does it (which can then give it back, if it doesn't need ownership of the value any more and doesn't want to free it itself). This even applies to the `drop` function, which just takes ownership and then does nothing whatsoever to the passed-in value, allowing it to go out of scope immediately. So once you've called `drop(foo)`, you no longer own the value and so can't give it away to any reading or writing function, and you can't drop it for the very same reason.