[rust-dev] No more obj { drop {} } -- use resources!

Marijn Haverbeke marijnh at gmail.com
Fri Jul 29 05:12:13 PDT 2011


As was decided when resources were introduced (I hope most of you were
informed), we wanted to drop support for object destructors. Resources
are now mostly useable -- though if you want to store or pass them
somewhere, you have to box them immediately on creation until we get
our move semantics worked out, or you'll get weird errors about
copying noncopyable types—or even code that silently does the wrong
thing!

resource my_cleanup(x: my_pointer_type) {
    my_free(x);
}

fn test() {
    // This is safe, cleans up the pointer at end of block
    {
        let x = my_cleanup(my_mk_pointer());
        do_stuff(*x); // Pass raw pointer
    }

    // This is safe, will clean up when refcount hits zero
    sink(@my_cleanup(my_mk_pointer()));

    // Don't do this (yet)
    sink_2(my_cleanup(my_mk_pointer()));
}


More information about the Rust-dev mailing list