Error

"future cannot be sent between threads safely" — How to Fix

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();