In rust sharing data is done via Arc (from libsync). Basic Arc is for sharing immutable data, for mutable there are MutexArc and RWArc. Sharing with Arc is copy free.
I put together a small example:
extern mod sync;
use std::hashmap::HashMap;
fn main() {
let arc = sync::RWArc::new(HashMap::<~str, int>::new());
arc.write(|m| m.insert(~"a", 0));
for num in range(1, 10) {
let arc = arc.clone();
spawn(proc() {
println!("[task {}] Value before is: {}", num, arc.read(|m| m.get(&~"a").clone()));
arc.write(|m| { m.insert_or_update_with(~"a", 0, |_, val| *val += 1); });
println!("[task {}] Value after is: {}", num, arc.read(|m| m.get(&~"a").clone()));
});
}
}
For the latest version of rust (0.10pre) use
extern crate collections;
extern crate sync;
use collections::hashmap::HashMap;
use sync::RWArc;
fn main() {
let arc = RWArc::new(HashMap::<~str, int>::new());
arc.write(|m| m.insert(~"a", 0));
for num in range(1, 10) {
let arc = arc.clone();
spawn(proc() {
println!("[task {}] Value before is: {}", num, arc.read(|m| m.get(&~"a").clone()));
arc.write(|m| { m.insert_or_update_with(~"a", 0, |_, val| *val += 1); });
println!("[task {}] Value after is: {}", num, arc.read(|m| m.get(&~"a").clone()));
});
}
}