Re:An Overview of Memory Management in Rust
by steveklabnik on Tuesday March 19, 2013 @12:10AM
(#5398880)
Rust tasks cannot share anything. Smart pointers go on the 'exchange stack', which is technically shared by tasks, but since there's only one reference to anything in the stack, it's okay.
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()));
}
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:
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.')