Shared State
use std::sync::{Arc, Mutex}; use std::thread; fn main() { let shared_value = Arc::new(Mutex::new(String::new())); let mut handles = vec![]; for i in 0..10 { let shared_value = Arc::clone(&shared_value); let handle = thread::spawn(move || { let mut s = shared_value.lock().unwrap(); s.push_str(&i.to_string()); }); handles.push(handle); } for handle in handles { handle.join().unwrap(); } println!("Result: {}", *shared_value.lock().unwrap()); }