Fix the 'future cannot be sent between threads safely' error by wrapping data in Arc<Mutex<T>> to ensure thread-safe ownership.
Wrap the non-thread-safe data in Arc<Mutex<T>> or Arc<RwLock<T>> to make it Send and Sync.
use std::sync::{Arc, Mutex};
let data = Arc::new(Mutex::new(vec![1, 2, 3]));
let handle = std::thread::spawn(move || {
let mut num = data.lock().unwrap();
num.push(4);
});
handle.join().unwrap();
The "future cannot be sent between threads safely" error occurs when you try to move data between threads that isn't safe to share. Think of it like trying to pass a handwritten note to someone else while you are still writing on it; you need a secure container (like a Mutex) to ensure only one person writes at a time, and a reference counter (Arc) to let multiple threads hold a copy of that container safely.