Re:An Overview of Memory Management in Rust
by dbaupp on Tuesday March 19, 2013 @01:40AM
(#5399165)
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).
[ Parent
| on HN ]
Re:An Overview of Memory Management in Rust
by minamea on Tuesday March 19, 2013 @01:47AM
(#5399187)
Reference please? I think this is wrong.
pcwalton 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
Re:An Overview of Memory Management in Rust
by minamea on Tuesday March 19, 2013 @02:51PM
(#5402823)
My bad folks :)
Re:An Overview of Memory Management in Rust
by pcwalton on Tuesday March 19, 2013 @01:59AM
(#5399219)
It's true. Returning a `~` pointer transfers ownership (so your caller will be responsible for cleaning it up).
Re:An Overview of Memory Management in Rust
by TheCoelacanth on Tuesday March 19, 2013 @09:09AM
(#5400610)
~ is similar to std::unique_ptr in C++. It is always owned in one place, but that ownership can be transferred at will.