3 ms·
A ~ pointer can still be returned from a function without being deallocated, and the contained data doesn't have to be copied (unlike data on the stack).
by dbaupp 14y ago
A ~ pointer can still be returned from a function without being deallocated, and the contained data doesn't have to be copied (unlike data on the stack).
- minamea 14y agoReference please? I think this is wrong.
- pcwalton 14y agoIt's true. Returning a `~` pointer transfers ownership (so your caller will be responsible for cleaning it up).
- dbaupp 14y agopcwalton is the voice of authority here, but to add to that, the following (verbose) example compiles and works as expected: fn make_string() -> ~str { return ~"foo"; } fn main() { let string: ~str = make_string(); io::println(string); } // deallocated here
- minamea 14y agoMy bad folks :)
- TheCoelacanth 14y ago~ is similar to std::unique_ptr in C++. It is always owned in one place, but that ownership can be transferred at will.