This commit is contained in:
2025-03-26 12:41:58 -04:00
parent 637b26a0e9
commit 416dd617e6
36 changed files with 732 additions and 0 deletions

1
example_8/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

6
example_8/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "example_8"
version = "0.1.0"
edition = "2021"
[dependencies]

23
example_8/src/main.rs Normal file
View File

@@ -0,0 +1,23 @@
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();
}