24 lines
571 B
Rust
24 lines
571 B
Rust
use std::rc::Rc; // reference-counted, non atomic
|
|
use std::sync::Arc; // reference-counted, atomic
|
|
|
|
fn main() {
|
|
// this will not compile:
|
|
let rc = Rc::new("not thread safe");
|
|
std::thread::spawn(move || {
|
|
println!("I have an rc with: {}", rc);
|
|
});
|
|
|
|
// this compiles fine:
|
|
let arc = Arc::new("thread safe");
|
|
std::thread::spawn(move || {
|
|
println!("I have an arc with: {}", arc);
|
|
});
|
|
|
|
// this will also not compile:
|
|
let mut v = Vec::new();
|
|
std::thread::spawn(|| {
|
|
v.push(42);
|
|
});
|
|
let _ = v.pop();
|
|
}
|