by minamea · 13y ago · 0 comments
Seems to me that the only difference between a smart pointer and declaring a variable is data on the heap vs data on the stack and there's not much difference there?
So you often create some sort of communications structure, keep one end yourself, and hand the other end off to a task. The smart-ness keeps this memory from being copied, which would be slow, but keeps you safe at the same time. Like this:
use task::spawn;
use pipes::stream;
use pipes::Port;
use pipes::Chan;
fn main() {
let (port, chan): (Port<int>, Chan<int>) = stream();
do spawn |move chan| {
chan.send(10);
}
io::println(int::str(port.recv()));
}
More: http://www.rustforrubyists.com/book/chapter-07.html and http://www.rustforrubyists.com/book/chapter-08.html (I use the older-as-of-yesterday term 'owned pointer' rather than 'smart pointer.') fn make_string() -> ~str {
return ~"foo";
}
fn main() {
let string: ~str = make_string();
io::println(string);
} // deallocated here